Created
January 17, 2017 17:54
-
-
Save kole/2461611c3922546683c81b7d03c9f0b0 to your computer and use it in GitHub Desktop.
Return number in Fibonacci sequence at given index
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
// Fibonacci sequence for increased delay algorithm | |
// return number in sequence at given index | |
const fib = (a) => { | |
const arr = [0, 1]; | |
let i = a; | |
if (i > 30) { i = 30; } // safeguard against recursion | |
for (let n = 0; n < i; n++) { | |
const last = arr[arr.length - 1]; | |
const nextToLast = arr[arr.length - 2]; | |
const sum = nextToLast + last; | |
arr.push(sum); | |
} | |
return arr.pop(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment