Infinite loop with Fibonacci numbers

Hi everyone,

This is a general question. I am trying to solve the Fibonacci problem, but it seems that in one of my tries I created an infinite loop and no matter what I do my browser is stack. I can’t delete any of the code from the problem and I can’t stop it. Do you have any idea how I can unstack this so I can solve the problem?

Thank you!

I’m not sure why the code would run when you refreshed the page. Regardless, try going to this link: https://www.freecodecamp.com/challenges/sum-all-odd-fibonacci-numbers#?solution= function%20sumFibs(num)%20{ %20%20return%20num%3B%2F%2F } sumFibs(4)%3B and hopefully it helps. :slight_smile:

1 Like

Thanks for this link. It saved my life :smiley:

Those kind of problems occur sometimes and I think it’s important to learn how to detect and fix them as fast as possible in order to protect you code. There are problems that can help you do that, such as checkamrx and others. Maintaining your code is important and you should do that.
Good luck with it.
Michael.

Causing an infinite loop is a rite of passage for new developers apparently. So join the club.

I have the same issue however I`m fairly sure that the infinite Loop error is wrong, however the //noprotect still does not help… Can anybody find the error? The code works until num==1000

function fibNum(numToCheck) {
  //check if the toAdd number is a fibunacci number
  var firstNum = 1;
  var secondNum = 1;
  var tempNum;
  var checkNum = numToCheck;
  for(var i=0; i<=numToCheck; i++)  {
tempNum = secondNum + firstNum;
firstNum = secondNum;
secondNum = tempNum;
if(numToCheck == tempNum) {
  return true;
}

  }
}

function isOdd(oddNum) {
  //check if the number of the fibunacci row is Odd or not
  if(oddNum %2 != 0) {
return true;
  }
  else return false;
}


function sumFibs(num) {
  var sum =0;
  for(var i=0; i<=num; i++) {
if(fibNum(i)===true && isOdd(i)===true) {
  sum +=i;
}

  }
  return sum +2;//because they are left out in the first iteration of the fibNum function
}

sumFibs(1000);

huge thanks in advance!

The infinite loop check is also triggered by code that runs too long - that seems to be the case here when sumFibs(4000000) test case runs

I see the warning for your code in beta freecodecamp

but not in regular freecodecamp

There are a few problems in your code - e.g. fibNum does not always return a boolean

Add a few console.log statements and track the progress of your code in the devtools console

I suggest taking a step back and rethinking your approach to solving this problem - try solving a few test cases on paper before writing code