Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save mistical2008/3080f32c872651a287562573e4e076a8 to your computer and use it in GitHub Desktop.

Select an option

Save mistical2008/3080f32c872651a287562573e4e076a8 to your computer and use it in GitHub Desktop.
/**
* Fibonacci sequence generator.
* https://developer.mozilla.org/docs/Web/JavaScript/Guide/Iterators_and_Generators#Advanced_generators
*
* @return {Number}
*/
function* fibonacci() {
let [a, b] = [0, 1];
let current;
while (true) {
yield current = a;
a = b;
b += current;
}
}
// test
const fib = fibonacci();
Array(10).fill().forEach(_ => console.log(fib.next().value));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment