Sum All Odd Fibonacci Numbers - Stuck on sumFibs(75024)

Tell us what’s happening:
Hi there,
I can’t seem to figure out why my algorithm is failing only for sumFibs(75024), but works for all the others inputs.

Your code so far


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); // I get 135721 as an answer, but it should be 60696.

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/sum-all-odd-fibonacci-numbers/

Your while loop condition is comparing the wrong variable to num. You don’t care if the total is less than or equal to num. You care if the current odd Fibonacci Number is less than or equal to num.

2 Likes