 # Potential infinite loop - I am lost , need help

I’m able to pass all of the tests, but I’m receiving this error message:
Potential infinite loop detected on line 5. Tests may be failing because of this.

I don’t know what I’m doing wrong.

function sumFibs(num) {
var sequence = [1, 1];
var total = 0;

for (var i = sequence; sequence.length < num; i++) {
if (sequence[sequence.length - 1] + sequence[sequence.length - 2] <= num) sequence.push(sequence[sequence.length - 1] + sequence[sequence.length - 2])
}

for (var j = 0; j < sequence.length; j++) {
if (sequence[j] % 2 !== 0) {
total += sequence[j]
}
}
}

console.log(sumFibs(10));

Why are you storing all of the Fibonacci numbers? Do you need to store all of the values in order compute the sum?

I can’t figure out how to find the odd numbers without storing them. Is that what is causing the error?

You can certainly figure out the odd numbers without storing them. I just did it yesterday!

Your code is taking too long, which is triggering the infinite loop protection.

1 Like

Also, I believe that you want to stop when you hit a Fibonacci number that is greater than `num`. Your `for` loop condition isn’t quite right.

So what seems off to me is that you are creating a var sequence as an array of [1,1]. Then in the first part of the for loop you set i = sequence. The i=part is supposed to be set to a numeric value and you are setting it to an array.

It may be some new syntax in JavaScript that I am not aware of, but I have never seen it done that way before.

1 Like

I am no longer seeing an error after changing my for loop.

for (var i = 2; i < num; i++)

Thank you!

You are still doing too many iterations with that bound. For example, with `sumFibs(10)` your loop body executes 9 times, but your loop body should only execute 5 times. The problem only gets worse as `num` increases. For `sumFibs(144)` your loop body executes 143 times but should only execute 10 times.

You only need to loop until the calculated Fibbonacci number is greater than `num`.

I am still not able to figure out what I am doing wrong

Which challenge is this for? URL for it preferably.

1 Like

Your code passes now, but you are doing way more work than you need to in order to get the job done.

``````    for (var i = 2; i < num; i++)
``````

This means that you stop after `num - 2` iterations. But You really want to stop when you have computed a Fibbonacci number that is greater than `num`.

Both codes passed. I’m failing to understand what exactly you’re telling me to do.

If you are fine with the code passing, then feel free to leave your code alone.

If you would like your code to run faster, I would adjust the bounds on your loop so that you are not making so many extra iterations. Here is your code with some extra `console.log`s.

``````function sumFibs(num) {
var sequence = [1, 1];
var total = 0;

console.log("Loop that makes the Fibs")
for (var i = 2; i < num; i++) {
if (sequence[sequence.length - 1] + sequence[sequence.length - 2] <= num) sequence.push(sequence[sequence.length - 1] + sequence[sequence.length - 2])
console.log(sequence[sequence.length - 1]);
}
console.log("Loop complete\n")

for (var j = 0; j < sequence.length; j++) {
if (sequence[j] % 2 !== 0) {
total += sequence[j]
}
}
To really see why your code can be slow, make `10` larger!