This question is not about the course, although I am taking it. In Elegant Javascript, there is an exercise that gave me trouble. I only got the answer by sort of copying an earlier example, but I don’t know why it works. Hopefully someone can explain.

Basically, the idea is to test whether a given number is even or odd using the following concepts: 1 is odd, 0 is even, for any other number, n, the evenness is the same as n-2.

I translated that into the following:

function isEven(number) {

if (number == 0) {

return true;

} else if (number == 1) {

return false;

} else {

isEven(number-2); //recursively calls the function on number-2; eventually, we’ll get down to 0 or 1 and get a true or false.

}

};

which doesn’t work. But what works is only one word different. It uses “return” for the else code-block:

function isEven(number) {

if (number == 0) {

return true;

} else if (number == 1) {

return false;

} else {

return isEven(number-2); //“return” makes all the difference

}

};

I don’t understand why you need the return. Wouldn’t calling the function eventually result in a return of either true or false above?