Created
March 4, 2015 19:32
-
-
Save tifletcher/d518d0c58f1ceec75f55 to your computer and use it in GitHub Desktop.
simple javascript generator without yield
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
// 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