Skip to content

Instantly share code, notes, and snippets.

@langley-agm
Last active September 10, 2017 02:33
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 langley-agm/873031748f5be3a8211920e5817de142 to your computer and use it in GitHub Desktop.
Save langley-agm/873031748f5be3a8211920e5817de142 to your computer and use it in GitHub Desktop.
JS Fibonacci
function fibonacci(n) {
var last = 0, next = 0, fibo = [];
while( n-- > 0 ) {
fibo.push(next);
next = last + (last = next) || 1;
}
return fibo;
}
console.log(fibonacci(0));
console.log(fibonacci(1));
console.log(fibonacci(2));
console.log(fibonacci(3));
console.log(fibonacci(10));
@langley-agm
Copy link
Author

Output:

[]
[0]
[0, 1]
[0, 1, 1]
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment