Recursion function

Please. Explain to me how this function works. Thank you.

Hi @SerafimPoch

It’s essentially whittling down the number until it reaches either 0 or 1, 0 meaning its an even number, 1 being odd.

A simpler example might be to use isEven(4).

1st call, 4 meets none of the if statements so it returns isEven(2), 4 - 2 being the 2 in this case.
2nd call, 2 is the same as 4 (no conditions met) so it returns isEven(0)
3rd call, first if condition is true so it returns true, this bubbles up to the 1st call and the function ultimately returns true, 0 is an even number.

If we used isEven(5), it would eventually meet the n === 1 condition and return false, 5 is an odd number.

The else if (n < 0) is there to catch any negative numbers, it will turn it into a positive number: -(-6) === 6.

As a side note, you can calculate an even number by using modulo: n % 2 === 0. Much easier and less verbose.

Essentially it will keep subtracting 2 from the number it is passed until the result is either 0, for even numbers, or 1 for odd. First, it’s checking if the number is already 0, then if it’s 1, then it makes sure the number is positive, and then it subtracts 2 and tries again. The last step of the function will keep calling itself until one of the first two statements is true, prompting it to return the boolean and exit the function.

1 Like

I stepped away for coffee while writing my reply and you beat me to it, sorry for the repeat explanation.

1 Like

Recursive functions break down larger problems into smaller ones.

This function takes a value n, checks for base cases which are:

if (n == 0) checks to see if n is equal to 0, if so the function returns true because it is even
else if (n == 1) checks to see if n is equal to 1, if so the function returns false because it is odd.

The function will keep calling itself until n reaches 1 or 0.

For example: if the function receives the number 5, it will not pass those two base cases because it is neither 1 or 0. The number 5 is also not less than 0, so it will not go to return isEven(-n) which would take a negative number and call the function again with that same number but turning it into a postive form. Now since it didn’t pass any of those, it would go to the else and you would get return isEven(5 - 2); which would call the function again with the number 3.

Repeat the process and you should return back to that else and it would call the function once more with the value 1. Then it would pass the base case of else if (n == 1) since it is equal to 1, returning the value false because the number 5 is not an even number. Hope that helps!


I understand. Thank you. But this part
else if (n < 0)
return isEven(-n);
I don’t. I guess I know what it does. But maybe be better if you explain in detail this part. Thanks

And way faster hahaha

1 Like

I know how it do more easier) I’ve trying to understand how recursion works)

1 Like

If you excluded else if (n < 0) and passed a negative number in: isEven(-4), the first 2 conditions would never be met so the function would never return and be caught in an infinite loop.

-4 === 0 // false
-4 === 1 // false

return isEven(-4 - 2) // This would just continually takeaway 2 from n, eventually leading to a max stack exceeded error.

By having else if (n < 0), it ensures that the above situation would never happen by turning the negative number into a positive: isEven(-4), would then recursively call isEven(4) and continue till it equals 0 and returns true.

I hope that makes sense


How exactly it turns -50 into 50 ?

    • = + ?

Prefixing - to a number is essentially shorthand for Math.abs(n).

Note, directly prefixing a number with - won’t work, you’d have to wrap it in parens, or add a space:

--4 // Uncaught ReferenceError
-(-4) // 4
- -4 // 4
Math.abs(-4) // 4

Oyyy I understand)) Thank you for your time)

1 Like

No worries dude, happy to help

1 Like

Maybe you recommend to me a better way to learn Js ? Because I’ve learn Js for two month but write functions it’s difficult to me. I undedrstand how functions works, but wrtite them to myself is kind of awkward :sweat:

I don’t really know of a better way to be honest, most of my learning came from doing with the guidance of various resources including freecodecamp.

These posts might be a good place to start: