Skip to content

Instantly share code, notes, and snippets.

@jamesxv7
Last active January 15, 2016 20:42
Show Gist options
  • Save jamesxv7/a87d66a6400db3c73abc to your computer and use it in GitHub Desktop.
Save jamesxv7/a87d66a6400db3c73abc to your computer and use it in GitHub Desktop.
The Fibonacci Sequence
/**
* 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