Sum All Odd Fibonacci Numbers Need a shorter code [Spoiler]

Sum All Odd Fibonacci Numbers Need a shorter code [Spoiler]
0

#1

Is there a way to increase the efficiency of the algorithm below:

function sumFibs(num) {
var fibArr = [1, 1];
var lastIndex = fibArr[fibArr.length - 1];
var sum = 0;

while(lastIndex <= num){
sum = fibArr.filter(function(val){
if (val % 2 === 0){
return false;
}else {
return true;
}
}).reduce(function(prev, curr){
return prev + curr;
});

lastIndex = fibArr[fibArr.length - 1] + fibArr[fibArr.length - 2];
fibArr.push(lastIndex);

}

return sum;
}

sumFibs(10);


#3

Thanks P1xt for the tip, it was helpful in making me see and think from a more efficient perspective🙌


#4

Hello, I have moved your post to the right category as it is not a wiki entry.