Created
November 3, 2023 06:38
-
-
Save FlameWolf/c5541a8bed2553627429c208e5baf27c to your computer and use it in GitHub Desktop.
JavaScript generator function to get Fibonacci numbers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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