Last active
May 27, 2020 19:39
-
-
Save niczky12/e3834acb82cf92ee39da478151735861 to your computer and use it in GitHub Desktop.
Fibonacci in JS
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 fib(n) { | |
var numbers = [0, 1] // initialise the array | |
while (n > 1) { | |
var new_num = numbers[0] + numbers[1]; | |
numbers.shift(); // drop the first element | |
numbers.push(new_num); // add the new element to the end | |
n -= 1; | |
} | |
return numbers[n]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment