Build an Odd Fibonacci Sum Calculator - Build an Odd Fibonacci Sum Calculator

Tell us what’s happening:

I am having issues with a lab regarding the Fibonacci Sequence. For some reason, the test case for sumFibs(4) should return a value of 5 (this pertains to the other test cases as well). However, that makes no sense as it wants all the odd numbers of the Fibonacci Sequence added up altogether then returned. Maybe my brain is burnt out because I admit this lab has burnt me out. I had to look up the best way to structure generating a Fibonacci Sequence because I couldn’t think.

Your code so far

function sumFibs(num) {
  let x1 = 0, x2 = 1, nextVal;
  const arr = [];
  let sum = 0;

  nextVal = x1 + x2;

  while (nextVal <= num) {
    arr.push(nextVal);

    x1 = x2;
    x2 = nextVal;
    nextVal = x1 + x2;
  }

  console.log("Current Array: " + arr)

  for (const element of arr) {
    if (element % 2 !== 0) {
      sum += element;
    }
  }

  return sum;
}

console.log("Current Sum: " + sumFibs(4));

Console Output:

Current Array: 1,2,3
Current Sum: 4

Challenge Information:

Build an Odd Fibonacci Sum Calculator - Build an Odd Fibonacci Sum Calculator

Hi @xSaberNight

The Fibonacci sequence starts with:

0,1,1,2,3,5

sumFibs(4)should return 5:

1+1+3

Happy coding

1 Like

Ah, I see. I failed to initialize the array with 0 and 1 to account for those values. This worked, thank you!

1 Like

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