Sum All Odd Fibonacci Numbers - Don't know what's wrong with this code

Tell us what’s happening:
I don’t know what’s wrong with this code of mine. I just tried something different but ain’t able to find where I did wrong. Please have a look.

Your code so far


function sumFibs(num) {
  //finds fibonacci in O(1) time
  function fib(val) {
    var phi = (1 + Math.sqrt(5)) / 2; 
    return Math.round(Math.pow(phi, val) / Math.sqrt(5)); 
  }
  var sum = 0;
  for(var i = 1; i <= num; i++) {
     var val = fib(i);
     while(val <= num) {
       if(val % 2 !== 0) {
         sum = sum + val;
       }
     }
     

  }

  return sum;
}


Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36.

Link to the challenge:

You are not changing val or num here, meaning this is an infinite loop

2 Likes

you need to add something that modifies the condition of the while loop. as long as the condition is true the loop will not break

2 Likes

Oh sorry! So, if I add an if statement before while loop, will it do?

if(val > num) {
       break;
     }

Really like your fibs calculator, though - remarkably useful.

2 Likes

why do you need that while loop, what is it that it is doing?

1 Like

Yeah sorry! While loop is not needed.

1 Like