Skip to content

Instantly share code, notes, and snippets.

@martingaido
Last active Jul 30, 2020
Embed
What would you like to do?
Function to create a Fibonacci sequence
/* Fibonacci Sequence */
const fibonacci = (result: number[], len: number) => {
let n1: number = result[0],
n2: number = result[1],
next: number,
cnt: number = 2;
while (cnt < len) {
next = n1 + n2;
n1 = n2;
n2 = next;
result.push(next);
cnt++;
}
return result;
}
console.log(fibonacci([0, 1], 20));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment