Sum All Odd Fibonacci Numbers - FCC bug?

Tell us what’s happening:
My code passes every test except for “sumFibs(4000000) should return 4613732.”
The confusing thing is that when I test the code on the browser’s console, it gives me the correct number.

Your code so far


function sumFibs(num) {
  function fibonacci (item) {
    if (item <= 1) {
      return 1;
    } else {
      return fibonacci(item - 1) + fibonacci(item - 2);
    }
  }
  var odds = [];
  var i = 0;
  while (fibonacci(i) <= num) {
    if (fibonacci(i) % 2 === 1) {
      odds.push(fibonacci(i));
    }
    i++;
  }
  return odds.reduce((acc,val) => acc + val);
}

sumFibs(4);

Your browser information:

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

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

Usually, if the big numbers fail the FCC test but generates the correct answer elsewhere (as your does) it’s because your algorithm isn’t efficient enough. The challenge has a timeout set and it your code takes too long, it exits early. Look for ways to make your algorithm more efficient.

1 Like

I didn’t consider about the efficiency. Thank you!