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?