Last active
August 10, 2018 17:23
-
-
Save delonnewman/7e454d49309c856fdde8b8a980d6bb94 to your computer and use it in GitHub Desktop.
An Introduction to Abstraction with Functions
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 square(x) { | |
return x * x; | |
} | |
function area(r) { | |
return Math.PI * square(r); | |
} | |
function add1(x) { | |
return 1 + x; | |
} | |
function fib(n) { | |
if (n <= 0) return 0; | |
else if (n === 1) return 1; | |
else { | |
return fib(n - 1) + fib(n - 2); | |
} | |
} | |
function add(a, b) { | |
return a + b; | |
} | |
[1, 2, 3].map(add1); // => [2, 3, 4] | |
[1, 2, 3].map(square); // => [1, 4, 9] | |
[1, 2, 3].map(fib); // => [1, 1, 2] | |
[1, 2, 3].reduce(add); // => 6 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment