Sum All Odd Fibonacci Numbers - For loop

I saw this solution in the comment section of a youtube video. I was wondering what they meant by i=newNum in the for loop. I wanted to solve it using a for loop but I kept ending up on a road block, any explanation for a for loop apprach would be appreciated.

function sumFibs(num) {
  var newNum = 0;
  var prevNum = 1;
  var sum = 0;
  for (var i = 1; i <= num; i = newNum) {

     newNum += prevNum;
     prevNum = i;
  if (prevNum % 2 !== 0) {
      sum += prevNum;

  return sum;

i=newNum is definitely not a good use of for loop.

If you want to change your iterator in a customized way, you should use a while loop, and change i variable inside of it.

1 Like

Noted thanks for that

1 Like