Skip to content

Instantly share code, notes, and snippets.

@pengisgood
Created December 3, 2018 07:11
Show Gist options
  • Save pengisgood/ebbd08a173d314b010d1ec9aed8c0b30 to your computer and use it in GitHub Desktop.
Save pengisgood/ebbd08a173d314b010d1ec9aed8c0b30 to your computer and use it in GitHub Desktop.
Calculate Fibonacci number with ES6 generator
const Fibonacci = function* (i = 1, j = 1) {
yield i;
yield j;
while (true) {
yield* Fibonacci(i + j, i + j + j);
}
};
const f = Fibonacci();
for (let i = 0; i < 20; i++) {
console.log('%s: %s', i+1, f.next().value);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment