Skip to content

Instantly share code, notes, and snippets.

@farskid
Last active October 26, 2017 00:12
Show Gist options
  • Save farskid/526943fc323a1b1be31b27d35800c65a to your computer and use it in GitHub Desktop.
Save farskid/526943fc323a1b1be31b27d35800c65a to your computer and use it in GitHub Desktop.
Log first N numbers in Fibonacci sequence in Javascript
function fibo(n) {
if (n === 1) return 0;
if (n === 2) return 1;
return fibo(n - 1) + fibo(n - 2);
}
function fiboLogger(n) {
if (n === 0) return [];
return [fibo(n)].concat(fiboLogger(n-1));
}
console.log(fiboLogger(10).reverse()) // [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment