Skip to content

Instantly share code, notes, and snippets.

@Shariar-Hasan
Last active September 7, 2023 21:59
Show Gist options
  • Save Shariar-Hasan/5a29667460b397ab9231eb38e5f7a439 to your computer and use it in GitHub Desktop.
Save Shariar-Hasan/5a29667460b397ab9231eb38e5f7a439 to your computer and use it in GitHub Desktop.
Print n-th value of fibonaci series or Print the whole fibonacci series til n-th value using recursive function in Javascript
// if you want whole fibonacci series till n
let fibb = [0, 1];
function fibonacciTillN(n) {
if (fibb[n - 1] != undefined) {
return fibb[n - 1];
} else {
return (fibb[n - 1] = fibonacciTillN(n - 1) + fibonacciTillN(n - 2));
}
}
fibonacciTillN(10);
console.log(fibb);
// Output:
// [0, 1, 1, 2, 3, 5, 8, 13, 21, 34];
// if you want to access any item,
//just call it with index,
//let assume you need 54th fibbonacci number
fibonacciTillN(100);
console.log(fibb[54]);
// Output:
// 86267571272
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment