Forked from anonymous/bonfire-sum-all-odd-fibonacci-numbers.js
Created
December 9, 2015 13:54
-
-
Save patrickcurl/392b6fc9c26f658c16bd to your computer and use it in GitHub Desktop.
http://www.freecodecamp.com/patrickcurl '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: @patrickcurl | |
// 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 a = 0, b = 1, c = 1, sum = 0; | |
var arr = [0]; | |
while (c <= num) { | |
arr.push(c); | |
c = a + b; | |
a = b; | |
b = c; | |
} | |
var odds = arr.filter(function(value, index, array){ | |
return value%2 == 1; | |
}); | |
var total = odds.reduce(function(a,b){ | |
return(a+b); | |
}); | |
return total; | |
} | |
sumFibs(4); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment