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

function sumFibs(num) {
  var sum=0;
  var a=[];
  for(var i=2;sum<=num;i++)
       { sum=sum+a[i];}
  return sum+1;

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).