Intermediate Algorithm Scripting - Sum All Odd Fibonacci Numbers

Hello guys!
I am stuck with this challenge, I am a newbie so I made up this and it only pass 5/6 and I don’t really know what to do anymore. Can you please take a look?
Thanks in advance.

function sumFibs(num) {
  let prev = 1;
  let curr = 1;
  let next;
  var sumofOdds = [];
  sumofOdds.push(prev);
  sumofOdds.push(curr);
  for(let i = 0; i<num; i++) {
    next = prev+curr;
    prev = curr;
    curr = next;
    // console.log(next)
    sumofOdds.push(next)
    // console.log(sumofOdds)
  };
  var odds = sumofOdds.filter((item) => item % 2 === 1);
  // console.log(odds);
  var total = 0;
for(let i = 0; i<odds.length - 1;i++) {
 while (total < num) {
   total +=odds[i];
   i++;
 };
};
  return total;
}

console.log(sumFibs(75025));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36

Challenge: Intermediate Algorithm Scripting - Sum All Odd Fibonacci Numbers

Link to the challenge:

This isn’t what num is for

Or this.

What do the instructions say num is for?

" Given a positive integer num , return the sum of all odd Fibonacci numbers that are less than or equal to num ." and : " For example, sumFibs(10) should return 10 because all odd Fibonacci numbers less than or equal to 10 are 1, 1, 3, and 5. "

Right. So neither of your usages are consistent with that. You need individual numbers that are less than num

1 Like

thanks. i re-write the last part according to your instructions. thank you very much sir.

var odds = sumofOdds.filter((item) => item % 2 === 1);
var finalodds = odds.filter ((item) => item <= num);
var total = 0;
console.log(finalodds)
for(let i = 0; i<finalodds.length;i++) {
total += finalodds[i];
}
return total;
}

1 Like

Good work! Now that you have something that works, I think this is a good challenge to compare the posted solutions with your own - there are some ways to be a bit more efficient.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.