JavaScript recursion: just asking for hints!

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));

Thank you for your help. :slight_smile:

Hi bro.
Well, I am not sure of what you want but I see in the function below.
Anyway, I’ll help ya. So, I have a code that I made here.

function ifEven(a){
  if(a == 0){
  return true;
  }else if(a==1){
  return false;
  }else{
  return ifEven(a - 2)
  }
}
  
  console.log(ifEven(2));
  console.log(ifEven(5));

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:

2 != 0,
2 != 1,
2 - 2 = 0;
0 == 0, so true

5 != 0
5 != 1
5 - 2 = 3
3 != 0
3 != 1
3 - 2 = 1
1 != 0
1 == 1, so false

1 Like

Thank you so much man! :smiley: I get it …

You’re welcome. And one final tip!

If you have a function that returns value, it’s better to assign it into a variable
like, var test = ifEven(2);.

I don’t understand what you’re trying to say.

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.
  • 2 - 2 is 0.
1 Like

@ArielLeslie , totally agree… but I guess these kinds of solutions get easier to notice over some time and with practice.

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.)

1 Like

You can see them with practice, but this problem still an awkward and totally unnatural use case for recursion.

1 Like

Thanks for advice, I am sure there are some good YouTube tutorials on this.

1 Like

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).

In this case you don’t even need that much!

return a % 2 === 0;

I know, but I tried to solve it through the recursion.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.