Hello guys, I have read many times here on forum that we should be asking for hints instead of just copying the code from other sources. Therefore, here I am. I am going through the book called Eloquent JavaScript, and I am currently doing the recursion task: > Recursion > Your function will likely look somewhat similar to the inner find function in the recursive findSolution example in this chapter, with an if/else if/else chain that tests which of the three cases applies. The final else, corresponding to the third case, makes the recursive call. Each of the branches should contain a return statement or in some other way arrange for a specific value to be returned. When given a negative number, the function will recurse again and again, passing itself an ever more negative number, thus getting further and further away from returning a result. It will eventually run out of stack space and abort.

The thing is, I can solve this with just a few if/else statements, but when it comes to recursion, I cannot understand how to write the correct recursive call, since I am still learning recursion.

HERE IS THE CODE I WROTE:

function ifEven(a){
if(a == 0){
return true;
}else if(a==1){
return false;
}else{
return (RECURSIVE CALL I DONT UNDERSTAND)
}
return ifEven(a);
}
console.log(ifEven(2));

As you can see, a recursive function always call itselfs inside its code
So, ‘If your number is 0, it’s even’, ‘However, if it’s 1, it’s odd (not even)’

And a recursive function says too:
‘but if your number is none of them, I’ll do it again with your number - 2’

In technical terms, it tests the conditions and if none of them are true, then it calls the function again with your argument ‘a’ subtracted to 2

And if your run the exact code that i’ve sent to you, you’ll see that it will return

true
false

Because 2 is even and 5 is odd, and it knows it that way:

This is the sort of challenge that can be frustrating because there is a simple way to solve the problem, and trying to figure out how to jam recursion in there requires thinking of an approach that no one would actually use.

It looks like the arbitrary constraint of this challenge is to only compare to the values 0 and 1 and to act like % isn’t an available operator. Here is a hint for how I would solve this challenge:

Any even number minus 2 will give you an even result.

You start to learn the patterns over time. If you’re interested in wrapping your brain around recursion, I recommend looking for some good video courses on writing proofs by induction.

(For example, this one is similar to proving that all even numbers are divisible by 2.)

I mean yes, I would probably choose to solve this problem with if/else statement, but how do you mean awkward and unnatural? In what sense? I am asking this because I am not experienced that much when it comes to recursive functions.

99% of the time, you just don’t want to use recursion. If there is a simpler expression with a basic fixed length loop, then don’t use recursion. Recursion comes in handy in a small number of cases, mostly involving ‘tree’ data structures and some specific math problems (Euclidean algorithm - Wikipedia).