Created
August 18, 2020 17:02
-
-
Save kdubbels/765366bfe69ceb0bd697168aee35b539 to your computer and use it in GitHub Desktop.
We’ve seen that % (the remainder operator) can be used to test whether a number is even or odd by using % 2 to see 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 corr…
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
// We’ve seen that % (the remainder operator) can be used to test whether | |
// a number is even or odd by using % 2 to see 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 single parameter (a positive, whole number) and return a Boolean. | |
function isEven(number) { | |
if (number === 0){ | |
return true; | |
} else if (number ===1) { | |
return false; } | |
else { | |
return isEven(number-2) | |
} | |
} | |
isEven(0) // true | |
isEven(1) // false | |
isEven(2) // true | |
isEven(3) // false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment