Even / odd number check using the recursive function

Hello Campers!

I’ve recently started learning how to use functions in JavaScript. And while learning I also doing some small tasks to ensure I learned the material well. Today I’ve faced with the following task: check whether the number is even or odd using the recursive function. I also have to handle the cases when the number is negative.

Below is the first version, that only handles positive numbers:

let isEven = (num) => {
  if (num == 0) return "Number is even";
  else if (num == 1) return "Number is odd";
  else return isEven(num - 2);
}

console.log(isEven(50));
// → Number is even
console.log(isEven(75));
// → Number is odd

If I pass it negative number I’ll see an error: RangeError: Maximum call stack size exceeded

In order to handle negative numbers I’ve added an additional if statement to the top of the existing one. Here is the code:

// Your code here.

let isEven = (num) => {
  if (num < 0) return "Number is negative";
  else if (num == 0) return "Number is even";
  else if (num == 1) return "Number is odd";
  else return isEven(num - 2);
}

console.log(isEven(-50));
// → Number is even
console.log(isEven(75));
// → Number is odd
console.log(isEven(-1));
// → Number is negative

Now I wonder whether is there a way to make this function shorter?

It seems to me, that these part of code:

else if (num == 0) return "Number is even";
else if (num == 1) return "Number is odd";

can be somehow shorthanded. But for now I don’t know how to do that and whether it is possible.

Any ideas?

Link to the challenge with more detailed description: https://eloquentjavascript.net/03_functions.html#i_jxl1p970Fy

Have you had any experience with javascript’s ternary operators? That would shorten it considerably, but also make it a lot less readable to the inexperienced coder:

const isEven = (num) => num < 0 ? "Number is negative" : num === 0 ? "Number is even" ? num === 1 ? "Number is odd" : isEven(num-2);

That right there expands out to the exact same function you wrote. The first ternary is this:

num < 0 ? "Number is negative" : // blah blah blah

that evaluates the first expression, num < 0, and if it’s true, returns the first statement (between the ? and the :). If false, it returns anything after the : - which in this case is another ternary expression:

num === 0 ? "Number is even" : // blah blah blah again

again, the first part evaluates, and based on true or false, returns the first clause or continues evaluating the rest of the expression. And so on.

So for the sake of brevity, that will work. For the sake of readability… ish.

1 Like

Oh myy! I was not aware of I can use the sequence from couple of ternary operators :smiley:

Thank you very much!

yeah, ternaries are very useful, but they aren’t good w/r/t readability for anything except very simple cases. You can split out the formatting, that would make it much shorter, ex:

function numType (n) {
  switch (n) {
    case 1: return "odd";
    case 0: return "even";
    default: return "negative";
  }
}

function isEven (num) {
  return (num <= 1) ?  `Number is ${numType(num)}` : isEven(num - 2);
}
1 Like

Thanks! This is another great solution :slight_smile: