Use Higher-Order Functions map, filter, or reduce to Solve a Complex Problem in JS

Tell us what’s happening:
Describe your issue in detail here.
My code is giving an error : TypeError: res.push is not a function
Does anyone know why this error is coming?

   **Your code so far**
const squareList = arr => {
 // Only change code below this line
let newarr= arr.filter((item)=>item > 0 && Number.isInteger(item)).reduce((res,item)=>res.push(item*item),[])
 return newarr;
 // Only change code above this line
};

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

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

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

Link to the challenge:

Remember that array.push returns the new length of the array.
So when reducing, the first iteration might be on an array, but after that the accumulator will be the returned value of the previous iteration, meaning it will be a number and not an array.

And you cannot push on a number, hence the error.

In other words, make sure you always return an array. :sparkles:

so wouldn’t the accumulator be [25] after first iteration?

With your existing code, after the first iteration of the reduce method, the accumulator value would just be 1 and not an array, so the second iteration would cause the error.

The accumulator is whatever is returned from the previous iteration, so no.
You are returning a number, not an array. :slight_smile: