Skip to content

Instantly share code, notes, and snippets.

@tifletcher
Created March 4, 2015 19:32
Show Gist options
  • Save tifletcher/d518d0c58f1ceec75f55 to your computer and use it in GitHub Desktop.
Save tifletcher/d518d0c58f1ceec75f55 to your computer and use it in GitHub Desktop.
simple javascript generator without yield
// Project Euler problem 2
function makeFibSequence(a, b) {
// or ...
// var a = 0;
// var b = 1;
return function () {
var next = a + b;
a = b;
b = next;
return next;
};
}
var go2 = function () {
var max = 4000000;
var current = 0;
var sum = 0;
var nextFib = makeFibSequence(0, 1); // makeFibSequence();
while (current < max) {
current = nextFib();
if (current % 2 == 0) sum += current;
}
return sum;
};
console.log(go2());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment