Created
June 20, 2017 22:17
-
-
Save noroot/db20a68a7a3d92e4b40b58aa1ac12995 to your computer and use it in GitHub Desktop.
Javascript Fibonacci sequence examples with 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 *fib() { | |
var temp, num1 = 0, num2 = 1; | |
while (1) { | |
yield num1; | |
temp = num1; | |
num1 = num2; | |
num2 += temp; | |
} | |
} | |
// fibonacci generator function | |
var genFib = fib(); | |
// Print the first ten numbers in the fibonacci sequence. | |
for (var arr = [], i = 0; i < 10; i++) { | |
arr.push(genFib.next().value); | |
} | |
let str = arr.join("\n"); | |
console.log("1st 10 fibonacci sequence numbers:\n" + str); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment