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;
}
A few things:
- This doesn’t change the result, but it looks nicer to use
var a = [0,1];instead ofvar a = []; a[0] = 0; a[1]=1; - You start with
sum = 0(but it is actually 1) and returnsum + 1. But this messes up your condition ofsum <= num. That is whysumFibs(4)fails - You have
sum <= numas loop condition, but that does not guarantee thata[i]that you will calculate in that loop will also be smaller thannum. So you will have to check for that. That should fixsumFibs(75024).