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

Tell us what’s happening:

Describe your issue in detail here.
I know my answer is correct here but just out of curiosity I am asking that “Why I was unable to do num * num in filter function”? Is filter function built like that only?

Your code so far

const squareList = arr => {
  // Only change code below this line
  let filteredList=arr.filter((num)=>{
    if(num>0 && Number.isInteger(num)){
      return num
    }
  })
  let squareList=filteredList.map(num=>num*num)
  return squareList;
  // 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/119.0.0.0 Safari/537.36

Challenge Information:

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

I’m not sure how looked the version that didn’t work. Could you show it in code?

Sure I was asking for this

const squareList = arr => {
  // Only change code below this line
  let squareList=arr.filter((num)=>{
    if(num>0 && Number.isInteger(num)){
      return num * num
    }
  })
  // Only change code above this line
};

const squaredIntegers = squareList([-3, 4.8, 5, 3, -3.2]);
console.log(squaredIntegers);

filter keeps or removes elements based on the callback function, it doesn’t replace elements with the result of callback function.

2 Likes

What Sanity said, Filter only take true or false (or “truthy”, link to mdn). If true, it kept the value. If false, it filters the value out of the array.

you could test this out:

arr = [1, 2, 3, 4, 5]
console.log[...arr].filter((number) => false)

// []
1 Like

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