The code from the Learn JavaScript the Hard Way module JavaScript Level 1 exercises. This is a mirror of the code I have in the book, so if you're struggling you can use this to compare against your attempts.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

26 lines
467 B

// simple recursive counting function
const counter = (i, upto) => {
if(i < upto) {
console.log("counter", i, upto);
counter(i+1, upto);
}
}
// use counter to just print out 1-5
counter(1, 5);
// a more complex counter that can call a callback
const cb_counter = (i, upto, cb) => {
if(i < upto) {
cb(i, upto);
cb_counter(i+1, upto, cb);
}
}
// do the same thing but with a callback
cb_counter(1, 6, (i, j) => {
console.log(i, j)
});