Intermediate Algorithm Scripting - Problem with sum of array elements

Intermediate Algorithm Scripting - Problem with sum of array elements

Hi! I have a problem with the sum of elements in my array of odd Fibonacci numbers. The code cases the test for sumFibs(1) and sumFIbs(4) but doesn’t for the rest of the numbers. I don’t know what’s wrong. Please help :slight_smile:

function sumFibs(num) {

var fib =[];
fib[0] = 0;
fib[1] = 1;
for (var i = 2; i <= num; i++) {
  fib[i] = fib[i - 2] + fib[i - 1];

var newArr = fib.filter(function(number)
  return number % 2 !== 0;

return newArr.reduce((x, y) => x + y)


I think the problem you’re having is you misunderstood what the challenge is asking you to do.

The challenge asks you to write the sum of all of the odd numbers in the Fibonacci sequence less than the number given (i.e. ‘num’).

Your code builds an Array of ‘num’ length and sums all of the odd numbers in that array.

For example:

[ 1, 1, 2, 3, 5, 8, 13, 21, 34, 55] //(This is 10 element array)

The challenge should be sumFibs(10) would add up until 8, since 13 is more than 10. It would disqualify 2 and 8 (from the summation) and give you 10 as the correct answer.

Your current code gives you the sum up to element 10 (‘55’) at the end. Hopefully, this helps.

Also, it’s computationally better to just disqualify the even numbers as you’re building your Fibonacci array, rather than after the fact.

Thank you vey much for your reply :slight_smile: I think I understand my mistake.