Created
December 3, 2018 07:11
-
-
Save pengisgood/ebbd08a173d314b010d1ec9aed8c0b30 to your computer and use it in GitHub Desktop.
Calculate Fibonacci number with ES6 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
const Fibonacci = function* (i = 1, j = 1) { | |
yield i; | |
yield j; | |
while (true) { | |
yield* Fibonacci(i + j, i + j + j); | |
} | |
}; | |
const f = Fibonacci(); | |
for (let i = 0; i < 20; i++) { | |
console.log('%s: %s', i+1, f.next().value); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment