Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save patrickcurl/392b6fc9c26f658c16bd to your computer and use it in GitHub Desktop.
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
// 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