Skip to content

Instantly share code, notes, and snippets.

@evillegas92
Created May 11, 2024 21:59
Show Gist options
  • Save evillegas92/63fb89d9c3bbe9e0c727dcb6f7826254 to your computer and use it in GitHub Desktop.
Save evillegas92/63fb89d9c3bbe9e0c727dcb6f7826254 to your computer and use it in GitHub Desktop.
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