1

I am learning JavaScript through Eloquent JavaScript and one of the exercises is to write a recursive function, isEven, that returns true if a number is even or false if a number is odd.

If I understood correctly, the author specifically wanted the following to be implemented:

  1. If a number == 0, then it is even.
  2. If a number == 1, then it is odd.
  3. "For any number N, its evenness is the same as N-2".

But when I use the code I have below, I get an error: InternalError: too much recursion (line 3 in function isEven) … How can I fix this while still using a recursive function?

// Your code here.
function isEven(n){
  if(n==0){
    return true;
  }
  else if(n==1){
    return false;
  }
  else{ 
    n = n-2; 
    isEven(n);  
  }  
}

console.log(isEven(50));
// → true
console.log(isEven(75));
// → false
console.log(isEven(-1));
// → ??
5
  • 1
    You're missing return in the else block. Use return isEven(n); Commented Jun 6, 2017 at 7:27
  • Both of the conditions will never be true with negative starting value ... Commented Jun 6, 2017 at 7:30
  • if it is negative make it position and solve. Commented Jun 6, 2017 at 7:31
  • @Tushar Good catch, thank you. And yes, you guys are right. I should have checked for negative numbers. Commented Jun 6, 2017 at 7:34
  • 1
    Be aware that with the recursive definition if you give it a large enough number it will indeed "run out of stack space". As a training exercise that's fine, but illustrates a point in the "real world" use of recursion that you must always bear in mind. Commented Jun 6, 2017 at 7:34

3 Answers 3

3

You could add another check, before decrementing/incrementing a value.

function isEven(n) {
    if (n == 0) {
        return true;
    }
    if (n == 1) {
        return false;
    }
    if (n > 0) {
        n = n - 2;
    } else {
        n = n + 2;
    }
    return isEven(n);
}

console.log(isEven(50));
console.log(isEven(75));
console.log(isEven(-1));

Sign up to request clarification or add additional context in comments.

Comments

1

To handle it recursion with that function, the value needs to be its absolute value.

console.log("isEven");

function isEven(n) {
  //Ensure that we look at the numbers absolute value
  n = Math.abs(n);
  //Do a loop instead of recursion
  if (n == 0) {
    return true;
  } else if (n == 1) {
    return false;
  } else {
    n = n - 2;
    return isEven(n);
  }
}
console.log(isEven(50));
console.log(isEven(75));
console.log(isEven(-1));
console.log("fasterIsEven");
//A faster way that eliminates recursion
function fasterIsEven(n) {
  return n % 2 === 0;
}
console.log(fasterIsEven(50));
console.log(fasterIsEven(75));
console.log(fasterIsEven(-1));

Javascript has a build-in method to test if something is dividable with something else, called modulus (%). This method is faster, but not recursive.

1 Comment

If you really want that last bit of performance, replace the % with a &. This is a bitwise and, which is a lot faster than the costly modulo that JS uses for floating point numbers!
-1

function IsEven(n){ return n%2 === 0 }

The core code is this one return n%2 === 0

In order to increase the program's strength, it is recommended to increase non-number decisions.

5 Comments

That's no longer a recursive function.
It has to be recursion, right? Even if it's inefficient?
Yupp. I believe the point of this exercise is to teach recursion. Your method is clearly way more efficient, only it doesn't solve the matter at hand :)
First, I suggest you change it,return isEven(n%2); Second,Other types of (float), thrid, swich Instead of if
Well I believe to teach recursion, it's better to use a concept that is easier to solve via recursion like iterating through trees, rather than teaching bad coding practices like using recursion where it's not needed.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.