Last active
November 22, 2015 04:00
-
-
Save minsooshin/161bf2e0c3b5bc7ba1b4 to your computer and use it in GitHub Desktop.
http://www.freecodecamp.com/minsooshin 's solution for Bonfire: Sum All Odd Fibonacci Numbers
This file contains hidden or 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
// Bonfire: Sum All Odd Fibonacci Numbers | |
// Author: @minsooshin | |
// Challenge: http://www.freecodecamp.com/challenges/bonfire-sum-all-odd-fibonacci-numbers | |
// Learn to Code at Free Code Camp (www.freecodecamp.com) | |
function sumFibs(num) { | |
var fibs = getFibs(num), | |
oddFibs = []; | |
function getFibs(max) { | |
var fib = [1, 1]; | |
for (var i = 2; i <= max; i++) { | |
fib[i] = fib[i - 2] + fib[i - 1]; | |
if (fib[i] > max) return fib; | |
} | |
return fib; | |
} | |
fibs = fibs.slice(0, fibs.length - 1); | |
oddFibs = fibs.filter(function(val) { | |
return (val % 2) === 1; | |
}); | |
return oddFibs.reduce(function(sub, curr) { | |
return sub + curr; | |
}); | |
} | |
sumFibs(75025); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment