Skip to content

Instantly share code, notes, and snippets.

@martingaido
Last active July 30, 2020 22:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martingaido/456d61501a2d0e0ce688a88b439e615f to your computer and use it in GitHub Desktop.
Save martingaido/456d61501a2d0e0ce688a88b439e615f to your computer and use it in GitHub Desktop.
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