Why the Array.reduce() reduce undefined?

Tell us what’s happening:
what is wrong on my reduce function? I returned the sum undefined no matter what I do!
I tried to make it arrow function but it points syntactic error under the if statement

Your code so far


function fiboEvenSum(n) {
let fibonacci=[1,2];
for ( let i=0;i<n-2;i++){
  fibonacci.push(fibonacci[i]+fibonacci[i+1]);
}
return fibonacci.reduce(function(sum,i){
   if (i%2===0){
      //sum+=i
    return sum+i;
   }
},0);
}

console.log(fiboEvenSum(10));

Your browser information:

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

Challenge: Problem 2: Even Fibonacci Numbers

Link to the challenge:
https://www.freecodecamp.org/learn/coding-interview-prep/project-euler/problem-2-even-fibonacci-numbers

what if i is odd? the callback function doesn’t return anything, and sum becomes undefined (the sum variable holds the returned value from callback running with previous i value)

what should the callback return when i is odd?

1 Like

In your solution this is the part that is causing problem.

return fibonacci.reduce(function(sum,i){
   if (i%2===0){
      //sum+=i
    return sum+i;
   }
},0);

When the loop will first iterate i=1, sum=0;
as i is not divisible by 2 hence the return value would be undefined which would then become the sum.
Next loop sum=undefined, i=2.(Now you have a problem and this would return undefined)
So my suggestions are instead invoking reduce on fibbonacci make an array of numbers divisible by to and then invoke on it. I can’t post the full solution. It is against freecodecamp

1 Like

I knew the normal for loop way, but I was practicing the high-order functions
The are great but they don’t apply for some situations

they apply if you make sure to use them correctly! in your case you need to make sure that the reduce callback always return something, not that it returns something only when i is even

Some information about reduce:-
When you return the value in this function then it becomes the value of the first argument(sum in your case)
When you write

fibonacci.reduce(function(sum,i){
   if (i%2===0){
      //sum+=i
    return sum+i;
   }
},0);

First the value of sum=0(the second argument you passed to reduce) and i=1(taken from the array you pass in this case fibonacci).
Now we go inside the loop. In this case i is not divisble by 2. Hence the return value would be undefined.(As you have no return value outside the if(statement)) which becomes the value of sum. Hence on next loop iteration value of sum is undefined(citing the info I gave you very first about reduce function)

after I read your comments guys, the code ran and test passed
checkout the code

function fiboEvenSum(n) {
  let fibonacci=[1,2];
  for ( let i=0;i<n-2;i++){
    fibonacci.push(fibonacci[i]+fibonacci[i+1]);
  }
  
  return fibonacci.reduce((sum,i)=>{
   return i%2===0? sum+i: sum;
 },0);
}

fiboEvenSum(10);