 # Sum All Odd Fibonacci Numbers - works for all except last one

Hi,

I have the below code for Sum All Odd Fibonacci Numbers problem and it seems to work for every number except the last one. Could someone tell me where I have gone wrong?

Here’s my code:

``````function sumFibs(num) {
var i = 1;
var base = [1,1];

while(i<=base.length){
var nextDigit = base[i]+base[i-1];
if(nextDigit < num){
base.push(nextDigit);
}
i++;
}

var sum = base.filter(function(x){return x % 2 !== 0;});
return sum.reduce(function(a,b){return a+b;});
}

sumFibs(75025);
``````

Thanks!

Given a positive integer num, return the sum of all odd Fibonacci numbers that are less than or equal to num.

facepalm

That explains it!

Thanks!

I solved it like this,

function sumFibs(num) {

``````var total = 0; // var to hold the total of all the odd numbers
var sequence = ; //array to hold the odd numbers in the fibonacci sequence add the initial 
var a = 0, b = 1, f = 1; // variables to run the fibonacci sequence
``````
``````//loop through and get all the fibonacci numbers between 1 and num
for (var i = 1; i <= num; i++) {
f = a + b;
a = b;
b = f;
``````
``````    //if f is an odd number, and f is less than or equal to num push it to the
//sequence array
if (f % 2 !== 0 && f <= num) {
sequence.push(f);
}
}
//console.log(sequence);//test
``````
``````//total all the numbers in the sequence array
for (i = 0; i < sequence.length; i++) {
total += sequence[i];
}
``````
``````// num = total or just return total
num = total;
return num;
``````

}

Mine does seem rather basic compared to yours.