Sum All Odd Fibonacci - I tried but its not working for func(4) and func(75024) pls help

Sum All Odd Fibonacci - I tried but its not working for func(4) and func(75024) pls help
0

#1
function sumFibs(num) {
  
  var sum=0;
  //len=(num*4)/10;
  var a=[];
  a[0]=0;
  a[1]=1;
  for(var i=2;sum<=num;i++)
    {
      a[i]=a[i-1]+a[i-2];
      if(a[i]%2!==0)
       { sum=sum+a[i];}
      
    }
  return sum+1;
}

#2

A few things:

  • This doesn’t change the result, but it looks nicer to use var a = [0,1]; instead of var a = []; a[0] = 0; a[1]=1;
  • You start with sum = 0 (but it is actually 1) and return sum + 1. But this messes up your condition of sum <= num. That is why sumFibs(4) fails
  • You have sum <= num as loop condition, but that does not guarantee that a[i] that you will calculate in that loop will also be smaller than num. So you will have to check for that. That should fix sumFibs(75024).