DEV Community

chandra penugonda
chandra penugonda

Posted on

Check if a number is even using recursion

Problem statement

Check if a number is even.

input: 8
output: true

var isEven = function(n) {
   // start here
};
Enter fullscreen mode Exit fullscreen mode

Solution:

var isEven = function (n) {
  if (n === 0) return true;
  else if (n === 1) return false;
  else {
    return isEven(n - 2);
  }
};
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
jayjeckel profile image
Jay Jeckel • Edited

Interesting. If there was a contest for the worst way to check for evenness, then using recursion would definitely win the award.