Created
May 11, 2024 21:59
-
-
Save evillegas92/63fb89d9c3bbe9e0c727dcb6f7826254 to your computer and use it in GitHub Desktop.
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() { | |
let prev = 0; | |
let next = 1; | |
yield prev; | |
yield next; | |
// Add previous and next and yield them forever | |
while (true) { | |
const newValue = next + prev; | |
yield newValue; | |
prev = next; | |
next = newValue; | |
} | |
} | |
const fib = fibonacci(); | |
for (let i = 0; i < 10; i++) { | |
const fibValue = fib.next().value; | |
console.log(fibValue); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment