Skip to content

Instantly share code, notes, and snippets.

@wongyouth
Created May 4, 2018 07:23
Show Gist options
  • Select an option

  • Save wongyouth/85aea076d643f0629bcb8b78e79ccf03 to your computer and use it in GitHub Desktop.

Select an option

Save wongyouth/85aea076d643f0629bcb8b78e79ccf03 to your computer and use it in GitHub Desktop.
Use generator to calc fabonacci in javascript
function * fab (max) {
let [n, a, b] = [0, 0, 1]
while (n++ < max) {
[a, b] = [b, a + b]
yield b
}
}
for (let n of fab(5)) {
console.log(n)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment