Solutions for 3rd edition Eloquent JS?
Eloquent JavaScript 2nd Edition recursion exercise solution - Stack Overflow
Eloquent Javascript Exercises - Javascript Course 2021 - Moralis Academy Forum
Eloquent JavaScript ch5 - "Your own loop" solution (loop vs recursion) - Stack Overflow
Videos
I realize it's only a draft edition, and it's not officially out yet. However I am doing the exercises and can't find any solutions to them, except for the ones that stayed the same. Does anyone know if they're available somewhere?
In case you're feeling like helping me out, I'm currently kind of stuck, and described my problem on pastebin.
I'll publish the solutions I have so far on my GitHub, since it seems no one has done so yet.
Cheers.
I think the reason you are getting confused is because you're not understanding how recursion works. The reason it is n-2 is, it is taking the number and subtracting 2 until it is either a zero or one. Therefore giving us a true or false.
Hope that helps. Read over how the process of recursion works.
The alternative solution that doesn't use the modulos operator % or any other built in functions in JavaScript is provided below. This solution instead relies on using another recursion to change negative value of the number.
function isEven(number) {
if (number < 0) {
return isEven(-number);
} else if (number == 1) {
return false;
} else if (number == 0) {
return true;
} else {
return isEven(number - 2);
}
}
console.log(isEven(50)); // true
console.log(isEven(75)); // false
console.log(isEven(-1)); // false
Yes, that's the correct recursive implementation for the task. You didn't just hit the correct output by chance.
The else return is a bit odd though. I would have written either
function loop(value, test, update, execute) {
if (test(value)) {
execute(value);
return loop(update(value), test, update, execute);
} // else stop
}
or
function loop(value, test, update, execute) {
if (!test(value)) return; // stop
execute(value);
return loop(update(value), test, update, execute);
}
Initializer
The book solution creates a value variable and initializes it with start. In your case this happens automatically when you pass your first parameter. So far the two approaches are equivalent.
Test
The for loop calls test(value) as cycle test. Your approach does something similar, but calling test is the test for recursion. So far the two approaches are equivalent.
Cycle block
The for loop calls body(value). Your approach calls execute(value). So far the two approaches are equivalent.
The update
value is updated in the cycle after each iteration. The same happens in your code when you pass update(value) in your recursive call. So far the two approaches are equivalent.
Are they really equivalent?
Algorithmically, yes. Technically, no. Your recursive approach calls the function several times and uses the stack (of the memory) to store the function calls. In the case of a too large numbers of test, your code will crash. So, you have successfully implemented a recursive version of the book example (yay!) but you should try to avoid recursion in most cases.