Skip to content

Instantly share code, notes, and snippets.

@kdubbels
Created August 18, 2020 17:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kdubbels/765366bfe69ceb0bd697168aee35b539 to your computer and use it in GitHub Desktop.
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…
// 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