Skip to content

Instantly share code, notes, and snippets.

@dsngo
Forked from jfairbank/fibonacci-generator.js
Created January 30, 2022 08:55
Show Gist options
  • Save dsngo/b7e6b4402bb86fb77acb51dca0335af9 to your computer and use it in GitHub Desktop.
Save dsngo/b7e6b4402bb86fb77acb51dca0335af9 to your computer and use it in GitHub Desktop.
Fibonacci ES6 Generator
function *fibonacci(n) {
const infinite = !n && n !== 0;
let current = 0;
let next = 1;
while (infinite || n--) {
yield current;
[current, next] = [next, current + next];
}
}
let [...first10] = fibonacci(10);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment