Last active
August 29, 2015 14:16
-
-
Save kylebakerio/afef8ec47d02aba17e47 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function isEven(number) { | |
if (number === 0) return true; | |
else if (number === 1) return false; | |
else { | |
if (number < 0) return isEven(number + 2); | |
else if (number > 35663) return "number too big"; | |
else return isEven(number - 2); | |
} | |
}; | |
isEven(35663) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This was the prompt:
We’ve seen that % (the remainder operator) can be used to test whether a number is even or odd by using % 2 to check whether it’s divisible by two. Here’s another way to define whether a positive whole number is even or odd:
Zero is even.
One is odd.
For any other number N, its evenness is the same as N - 2.
Define a recursive function isEven corresponding to this description. The function should accept a number parameter and return a Boolean.
Test it on 50 and 75. See how it behaves on -1. Why? Can you think of a way to fix this?
// Your code here.
console.log(isEven(50));