Created
August 22, 2015 18:47
-
-
Save thinklinux/6808ac6a94cbe3cd2e97 to your computer and use it in GitHub Desktop.
Fibonacci with javascript
This file contains hidden or 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
// start should be 0 or 1 because by definition fibonacci sequence is starting from 0 element with 0,1 or from the first element with 1,1 | |
var start = 0// or 1; | |
var results = start == 0 ? [0, 1] : [1, 1]; | |
var i = 0; | |
var end = 10; // how far you want to go | |
while(i < end) { | |
var len = results.length; | |
var next = results[len-1] + results[len-2]; | |
results.push(next); | |
i = i + 1; | |
} | |
console.log(results.join(', ')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment