Skip to content

Instantly share code, notes, and snippets.

@NathanKleekamp
Last active September 16, 2019 12:34
Show Gist options
  • Save NathanKleekamp/38d9b4a10148f42176b8a6b349932083 to your computer and use it in GitHub Desktop.
Save NathanKleekamp/38d9b4a10148f42176b8a6b349932083 to your computer and use it in GitHub Desktop.
A recursive Fibonacci Sequence generator
const fib = (count, result = [0, 1]) => {
if (count <= 2 || count === 0) {
return result;
}
const { length } = result;
return fib(count - 1, result.concat(result[length - 1] + result[length - 2]));
};
const fib5 = fib(5) // [0, 1, 1, 2, 3]
const fib10 = fib(10) // [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