Skip to content

Instantly share code, notes, and snippets.

@FlameWolf
Created November 3, 2023 06:38
Show Gist options
  • Save FlameWolf/c5541a8bed2553627429c208e5baf27c to your computer and use it in GitHub Desktop.
Save FlameWolf/c5541a8bed2553627429c208e5baf27c to your computer and use it in GitHub Desktop.
JavaScript generator function to get Fibonacci numbers
function* fibonacci(max = Number.MAX_SAFE_INTEGER) {
let current = 1;
let previous = 0;
while (current <= max) {
[current, previous] = [previous, current + previous];
if (current <= max) {
yield current;
} else {
return;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment