Intermediate Algorithm Scripting - Sum All Odd Fibonacci Numbers

Tell us what’s happening:
Describe your issue in detail here.

I am not able to figure out what is wrong with the code. Why is not pushing the computed f3 values instead it pushes the index number.

Your code so far

function sumFibs(num) {
  let fib = [0, 1,1];
  let i = 3
  do {
    let f1 = fib[i - 1];
    let f2 = fib[i - 2];
    var f3 = f1+f2;
    console.log(fib.push(f3));
    i++;
  } while (f3 < num)
  return fib
    .filter(element => element % 2 !== 0)
    .reduce((a, b) => a + b);
}

console.log(sumFibs(4));



Your browser information:

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

Challenge: Intermediate Algorithm Scripting - Sum All Odd Fibonacci Numbers

Link to the challenge:

Why do you think index values are pushed?

your assumption maybe incorrect in so far that you console.log() the return value of fib.push(f3) inside your loop.

MDN states for Array.prototype.push():

Return value

The new length property of the object upon which the method was called.

You might also want to check which numbers are in your array, especially the last entry and compare it to the requirement again.

I am not able to figure that out. It would be kind of you to give me a hint about it

Pardon me, I am not able to understand what you mean . Could you please tell me what is the problem with my code in a different way.

remove the reduce from your return and look at the numbers, the requirement states

return the sum of all odd Fibonacci numbers that are less than or equal to num.

thank you for help. I figured it out.

1 Like

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