Skip to content

Instantly share code, notes, and snippets.

@kole
Created January 17, 2017 17:54
Show Gist options
  • Save kole/2461611c3922546683c81b7d03c9f0b0 to your computer and use it in GitHub Desktop.
Save kole/2461611c3922546683c81b7d03c9f0b0 to your computer and use it in GitHub Desktop.
Return number in Fibonacci sequence at given index
// 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