While loop old solution question

A post of a partial solution raised some questions that start in the while loop after the if statement. The if statement makes all a values odd. Outside of it a values are not always odd once they are mixed with b values.
In the outside loop why do the values start at what seems like the second run?

console.log(temp,a,b); //1,2,1 not 0,1,0
function sumFibs(num) {
  let total = 0;
  let a = 1;
  let b = 0;
  let temp;

  while (total <= num) {
    if ( a % 2 !== 0 ) {
      total += a;
    }
    temp = a;
    a += b;
    b = temp;
  }
  return total;
}

sumFibs(75024);

Sum All Odd Fibonacci Numbers - Stuck on sumFibs(75024) - JavaScript - The freeCodeCamp Forum

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

Challenge: Sum All Odd Fibonacci Numbers

Link to the challenge:

a fibonacci series start with 1,1, then each following number is the sum of the last two numbers

so you sum only the odd numbers, but you still need to use the even numbers to calculate the rest of the sequence

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.