Use Higher-Order Functions map, filter

have included comments in below code to explain problem
The solution i have works but i don’t understand why the first attempt, as shown , does not.

  **Your code so far**

const squareList = arr => {

const arrPosInt=arr.filter(elem=>{
  if(Number.isInteger(elem) && elem>0) {
    return elem*elem} //includes square function/statement, but....
})
console.log(arrPosInt) //....doesn't work. returns [ 5, 3 ]
//return arrPosInt;  // ..returns [ 5, 3 ]

const arrPosIntSq = arrPosInt.map(elem=>elem*elem) //have to square seperately..
return arrPosIntSq;  // ..returns [ 25, 9 ]
};

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

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

Link to the challenge:

sugar, is that due to double declaration of function at top??

what do you expect to see here instead of [5, 3]?

i expected 25, 9
as there is elem*elem

but filter doesn’t change the array elements, it just keeps or discard them

so the maths HAS to be outside the .filter function/expression?

filter is not the method to use for that, no

2 Likes

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