Last active
September 27, 2016 18:57
-
-
Save jurosh/e293fd022e83db0efd11832f5eb9a872 to your computer and use it in GitHub Desktop.
Fibonacci sequence - javascript generator
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 * fibon () { | |
let val1 = 0; | |
let val2 = 1; | |
let swap; | |
yield val1; | |
yield val2; | |
while (true) { | |
swap = val1 + val2; | |
val1 = val2; | |
val2 = swap; | |
yield swap; | |
} | |
} | |
// Usage like: | |
let fib = fibon(); | |
console.log(fib.next()); // 0 | |
console.log(fib.next()); // 1 | |
console.log(fib.next()); // 1 | |
console.log(fib.next()); // 2 | |
console.log(fib.next()); // 3 | |
... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment