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

Tell us what’s happening:

Everything passes - but i’m not happy with what i had to do to get it to pass.

Your code so far

function sumFibs(num) {
  let first = 0;
  let second = 1;
  let array = [0, 1];

  for (let i = 0; i < num; i++) {
    let third = first + second;
    if (third > num) {
      break
    }
    array.push(third);
    first = second;
    second = third;
    
  }
  
  const oddNumbers = array.filter(num => num % 2 !== 0);
  

  const sum = oddNumbers.reduce((accumulator, currentValue) => {
    return accumulator + currentValue;
}, 0);

  return sum;
}

The loop i’ve used, generates the right numbers and i get the right result. But it’s really messy and i don’t like it.

Can i please get some suggestions of what I should be doing with my loop?

Your browser information:

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

Challenge Information:

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

We have blurred this solution (with [spoiler][/spoiler] tags) so that users who have not completed this challenge can read the discussion in this thread without giving away the solution.

1 Like

I would never build the array. Your code will be both tidier and faster.