Skip to content

Instantly share code, notes, and snippets.

@masterots
Last active August 29, 2015 14:20
Show Gist options
  • Save masterots/b228ff0af90811a3da1c to your computer and use it in GitHub Desktop.
Save masterots/b228ff0af90811a3da1c to your computer and use it in GitHub Desktop.
Project Euler - Even Fibonacci numbers
function *fibbonacciGenerator(max) {
if (!max) {
console.log("Please enter a maximum number for the fibbonacciGenerator");
return;
}
var previous = 0;
var next = 1;
var current = 1;
while (current < max) {
yield current;
current = previous + next;
previous = next;
next = current;
}
}
function sumEvenValues(sum, numToAdd) {
if (numToAdd % 2 !== 0) {
return sum;
}
return sum + numToAdd;
}
var sum = 0;
for (var i of fibbonacciGenerator(4E9)) {
sum = sumEvenValues(sum, i)
}
console.log(sum);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment