Skip to content

Instantly share code, notes, and snippets.

@noroot
Created June 20, 2017 22:17
Show Gist options
  • Save noroot/db20a68a7a3d92e4b40b58aa1ac12995 to your computer and use it in GitHub Desktop.
Save noroot/db20a68a7a3d92e4b40b58aa1ac12995 to your computer and use it in GitHub Desktop.
Javascript Fibonacci sequence examples with generators
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