Last active
January 15, 2016 20:42
-
-
Save jamesxv7/a87d66a6400db3c73abc to your computer and use it in GitHub Desktop.
The Fibonacci Sequence
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
/** | |
* 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ... | |
* The next number is found by adding up the two numbers before it. | |
* - The 2 is found by adding the two numbers before it (1+1) | |
* - Similarly, the 3 is found by adding the two numbers before it (1+2), | |
* - And the 5 is (2+3), and so on | |
*/ | |
// Using recursion | |
function fibonacciNumberOf(number) { | |
return ( | |
number < 2 ? | |
number : | |
fibonacciNumberOf(number - 1) + fibonacciNumberOf(number - 2) | |
); | |
} | |
console.log(fibonacciNumberOf(7)); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment