Not passing one of the tests: Sum of Fibonacci numbers

Interestingly, all test cases are passing except the third one. I tried the code in hint section but the advanced solution, too, doesn’t pass a couple tests.

Can anyone please tell me where is the bug?

Your code so far


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

console.log(sumFibs(4000000));

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_2) 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

You’re doing enormous amounts of calculations and they’re all recursive. I haven’t tested the code, but this will definitely trip the timeout on the tests for very large inputs. Ideally find a solution that does not use recursion.

I see. Thanks, Dan. I’ll write a for loop.