Given a positive integer num, return the sum of all odd Fibonacci numbers that are less than or equal to num.
The first two numbers in the Fibonacci sequence are 1 and 1. Every additional number in the sequence is the sum of the two previous numbers. The first six numbers of the Fibonacci sequence are 1, 1, 2, 3, 5 and 8.
For example, sumFibs(10) should return 10 because all odd Fibonacci numbers less than 10 are 1, 1, 3, and 5.
sumFibs(1) should return a number.
sumFibs(1000) should return 1785.
sumFibs(4000000) should return 4613732.
sumFibs(4) should return 5.
sumFibs(75024) should return 60696.
sumFibs(75025) should return 135721.
return ??
//wrong :return ??
function sumFibs(num) {
var arrFibs = [1];
var sum = 0;
for(var i=1;i<=num;) {
arrFibs.push(i);
i = arrFibs[arrFibs.length-1] + arrFibs[arrFibs.length - 2];
}
arrFibs.reduce(function(prev,curr){
if(curr%2 !== 0) sum = prev + curr;
else sum = prev;
});
return sum;
}
sumFibs(4);```
![Wrong](http://upload-images.jianshu.io/upload_images/316258-1f3409376f4fee09.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
//正解
function sumFibs(num) {
var arrFibs = [1];
for(var i=1;i<=num;) {
arrFibs.push(i);
i = arrFibs[arrFibs.length-1] + arrFibs[arrFibs.length - 2];
}
return arrFibs.reduce(function(prev,curr){
if(curr%2 !== 0) return prev + curr;
else return prev;
});
}
sumFibs(4);```