Last active
August 29, 2015 14:21
-
-
Save beije/224cfb66c327f3e36f54 to your computer and use it in GitHub Desktop.
Fibonacci with ES6 generators
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 *fibonacci(numbers) { | |
var prevValue = 0; | |
var currentValue = 1; | |
var newValue = 0; | |
for(var i = 0; i < numbers; i++) { | |
newValue = currentValue+prevValue; | |
prevValue = currentValue; | |
currentValue = newValue; | |
yield currentValue; | |
} | |
} | |
for (var v of fibonacci(10)) { | |
console.log( v ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment