Problem 2: Even Fibonacci Numbers - passing but also failing :(

All testcases except this one is passing:

Your function is not returning the correct result using our tests values.

Any ideas what I’m doing wrong?

Your code so far


function fiboEvenSum(n) {
  let fibArray = [1,2];
  
  for(var i = 2; i <= n+1; i++){
    let subTotal = (fibArray[i - 2] + fibArray[i - 1]);
    fibArray.push(subTotal);
  }

  let evenFibArray = [];
  let evenSubTotal = 0;
  for(var j = 0; j < fibArray.length; j++){
    if(fibArray[j] % 2 === 0){
      evenSubTotal += fibArray[j];
      evenFibArray.push(fibArray[j])
    }
  }
  return evenSubTotal;
}

fiboEvenSum(10);

Your browser information:

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

Link to the challenge:

The problem is in your first loop. They are asking you for the first N fibonacci numbers, which you won’t get from i at all. Instead, you can get that from the length of fibArray:

for(var i = 2; fibArray.length <= n; i++){ ... }

Note that the only thing i changed from your code is the middle clause, the one used to break out of the for loop. Everything else is exactly fine.

2 Likes