Hello! I hope you could bear with me and explain me the error I got.
So far all the provided tests went through except for this one:
sumFibs(4000000) should return 4613732.
The reason for failing was provided as follow:
Error: potential infinite loop at line 7
I still haven’t seen why the code could produce an infinite loop (as I’ve set a condition i <= num and there’s no change made to num); and why the other tests went through…
My code so far
function sumFibs(num) {
var sum = 0;
var fib = [0, 1, 1];
//get array "fib" of Fibonaci numbers
for (var i = 2; i <= num; i++) {
if (i == (fib[fib.length - 1] + fib[fib.length - 2])) {
fib.push(i);
}
}
//sum all odd numbers in it
for (var j = 0; j < fib.length; j++) {
if (fib[j] % 2 != 0) {
sum += fib[j];
}
}
return sum;
}
sumFibs(4000000);
Link to the challenge:
https://www.freecodecamp.org/challenges/sum-all-odd-fibonacci-numbers