Sum All Odd Fibonacci Numbers loop not working right

Sum All Odd Fibonacci Numbers loop not working right
0

#1

Tell us what’s happening:
It appears that the loop is exiting after the first iteration every time. I can’t figure out why this is happening.

Your code so far

function sumFibs(num) {
  var sum = 1;
  for(var i = 1;i<= num;){
    if(i%2 !==0){
      sum +=i;
    }
    i +=i;
  }
  return sum;
}

sumFibs(4);

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (X11; CrOS aarch64 10032.86.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.140 Safari/537.36.

Link to the challenge:


#2

This is where your logic is going a bit sideways. You are multiplying i by 2 each time. Your loop iterates over powers of 2 so it very quickly is larger than num.

Look here: