Why does my reduce function recognize my accumulator variable as undefined?

Hi friends, my solution to this challenge is basically the same as the one in the provided solution (in my opinion). Only the if condition is written differently. Why does my code give the error “cannot read concat property of undefined”?

I’m sure the problem is simple but I can’t seem to figure it out. If you can point me to a direction, I will really appreciate it!

  **Your code so far**

const squareList = arr => {
 
return arr.reduce((squares,item)=>{
if (Number.isInteger(item) && item>0)
{squares.concat(item*item)}

},[])

};



const squaredIntegers = squareList([-3, 4.8, 5, 3, -3.2]);
console.log(squaredIntegers);
  **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36

Challenge: Use Higher-Order Functions map, filter, or reduce to Solve a Complex Problem

Link to the challenge:

the accumulator for next iteration will be the return value of the callback for the current iteration, your callback function doesn’t return anything, so the second time that the callback is called, squares has become undefined

4 Likes

This makes so much sense! Thank you so much for taking the time to answer my questions!

Best,

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