Functional Programming - Use Higher-Order Functions map, filter, or reduce to Solve a Complex Problem

Tell us what’s happening:
I get TypeError: sqrIntegers.push is not a function. Cannot push() method be used in reduce() method?

Your code so far

const squareList = (arr) => {
  // Only change code below this line
  return arr.reduce((sqrIntegers, num) => {
    return Number.isInteger(num) && num > 0
      ? sqrIntegers.push(num * num)
      : sqrIntegers;
  }, []);  

  
  // 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/98.0.4758.102 Safari/537.36

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

Link to the challenge:

Do you know what the return value of push is?

I have looked at some documents and it says push() returns ‘The new lengths property of the object upon which the method was called.’

I also read the explanation ‘concat returns a new array while push returns the length of updated array’ .

Can you please explain how the above difference give different outcome in this problem?

Thanks you for your help.

The callback for reduce needs to return the value that the array is being reduced into. push won’t do that in your ternary.

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