What is wrong with my Sum of odd fibonacci numbers?

Hello, I’m workingin the Sum All Odd Fibonacci Numbers, my code only passes 2 tests, but I don’t understand why.

function sumFibs(num) {
  let a = 1, b = 0, sum = 0, odds = [], temp;
  
  while (num >= 0) {
    if (b % 2 !== 0) {
      odds.push(b);
    }
    temp = a;
    a = a + b;
    b = temp;
    num--;
  }
  
  for (i = 0; i < odds.length; i++) {
    sum += odds[i];
  }
  
  return sum;
}

console.log(sumFibs(1000));

For instance, the array of odds with 4 is [1, 1, 3], with 10 it’s [1, 1, 3, 5, 13, 21, 55], but with 1000 it’s out of memory or something, because it’s the same as with 100:

100:

[1, 1, 3, 5, 13, 21, 55, 89, 233, 377, 987, 1597, 4181, 6765, 17711, 28657, 75025, 121393, 317811, 514229, 1346269, 2178309, 5702887, 9227465, 24157817, 39088169, 102334155, 165580141, 433494437, 701408733, 1836311903, 2971215073, 7778742049, 12586269025, 32951280099, 53316291173, 139583862445, 225851433717, 591286729879, 956722026041, 2504730781961, 4052739537881, 10610209857723, 17167680177565, 44945570212853, 72723460248141, 190392490709135, 308061521170129, 806515533049393, 1304969544928657, 3416454622906707, 5527939700884757]

1000

[1, 1, 3, 5, 13, 21, 55, 89, 233, 377, 987, 1597, 4181, 6765, 17711, 28657, 75025, 121393, 317811, 514229, 1346269, 2178309, 5702887, 9227465, 24157817, 39088169, 102334155, 165580141, 433494437, 701408733, 1836311903, 2971215073, 7778742049, 12586269025, 32951280099, 53316291173, 139583862445, 225851433717, 591286729879, 956722026041, 2504730781961, 4052739537881, 10610209857723, 17167680177565, 44945570212853, 72723460248141, 190392490709135, 308061521170129, 806515533049393, 1304969544928657, 3416454622906707, 5527939700884757]

Could someone point me in the right direction please?

I think you got to the limit of Number.

Edit: if you look at the tests you will see that you are way off. Not sure what’s wrong with your code, but you shouldn’t have any problems with too large values.

2 Likes

Beat me to it, I was just looking at the same article.

2 Likes

The num variable tells you the largest fib number to include, so if num is 89, your fibs should be [1, 1, 3, 5, 13, 21, 55, 89].

2 Likes

What it looks like is your code is taking the number provided and saying “Ok, generate num amount of Fibonacci numbers.” I used 10, and your code generated 0,1,1,3,5,8,13,21,55 - it only pushed the odd fibonacci numbers, but it still ran through 10 iterations. What you want is to only count with the Fibonacci numbers. Forgetting the odd numbers for simplicity, if I were to ask for the sum of all fibonacci numbers under 10, it should return 0, 1, 1, 2, 3, 5 and 8, with 8 being the last possible fib number that’s equal or less than 10. In a loop it would have run 7 times (or 6 if we don’t include the zero) to get the all of the fib numbers that are less than or equal to 10.

Hope that helps you!

1 Like

I managed to solve it, a guy pointed me out that I shouldn’t create a new array to store odd numbers, instead if the number was odd, add it to the sum :smile:

1 Like
+ while(b <= num)
- while(num >= 0)
- num--;

In your code, the loops will be num + 1, rather than depend on if b <= num.
For instance, if num = 100, it should loop 11, while your code will loop 101.