Created
February 3, 2015 04:04
-
-
Save chaseconey/bf6610a9f02060ee5318 to your computer and use it in GitHub Desktop.
// source http://jsbin.com/ginoqot
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
// 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? | |
// URL: http://eloquentjavascript.net/03_functions.html#p_iDq2OgBOGw | |
function isEven(num) { | |
num = Math.abs(num); | |
if (num === 0) { | |
return true; | |
} else if (num === 1) { | |
return false; | |
} | |
return isEven(num-2); | |
} | |
console.log(isEven(50)); | |
console.log(isEven(75)); | |
console.log(isEven(-1)); | |
console.log(isEven(-100)); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">// 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? | |
// URL: http://eloquentjavascript.net/03_functions.html#p_iDq2OgBOGw | |
function isEven(num) { | |
num = Math.abs(num); | |
if (num === 0) { | |
return true; | |
} else if (num === 1) { | |
return false; | |
} | |
return isEven(num-2); | |
} | |
console.log(isEven(50)); | |
console.log(isEven(75)); | |
console.log(isEven(-1)); | |
console.log(isEven(-100));</script></body> | |
</html> |
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 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? | |
// URL: http://eloquentjavascript.net/03_functions.html#p_iDq2OgBOGw | |
function isEven(num) { | |
num = Math.abs(num); | |
if (num === 0) { | |
return true; | |
} else if (num === 1) { | |
return false; | |
} | |
return isEven(num-2); | |
} | |
console.log(isEven(50)); | |
console.log(isEven(75)); | |
console.log(isEven(-1)); | |
console.log(isEven(-100)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment