Does filter does not allow operation like squarint etc?

Tell us what’s happening:

Inside filter method if we put Math.pow(currNum,2), why it return [5,3] and not [25,9]
Your code so far

const squareList = (arr) => {
// only change code below this line
let sqrtArray = arr.filter(currNum => 
  (currNum %1 ===0 && currNum >0)

return sqrtArray;
// only change code above this line

// test your code
const squaredIntegers = squareList([-3, 4.8, 5, 3, -3.2]);

Your browser information:

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

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

Link to the challenge:

Because .filter() only does filtering, i.e. if the callback function returns true, the element is added to the new array and discarded if the callback function returns false. No modification to the original array element is done.

filter returns a new array containing all the elements that pass a test. Math.pow(currNum,2) is not a test.

map returns a new array containing all the elements after they have had a function applied to them: in that case, the squaring function would be appropriate.

1 Like