DEV Community

chandra penugonda
chandra penugonda

Posted on

2 2

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.

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay