Last active
August 29, 2015 14:20
-
-
Save masterots/b228ff0af90811a3da1c to your computer and use it in GitHub Desktop.
Project Euler - Even Fibonacci numbers
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 *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