Another Solution for Use Higher-Order Functions map, filter, or reduce to Solve a Complex Problem - Beginner

Continuing the discussion from freeCodeCamp Challenge Guide: Use Higher-Order Functions map, filter, or reduce to Solve a Complex Problem:

I solved the given problem using the .filter() from the first solution, but instead of using Math.pow, I used just simple multiplication since not all Math methods have been shown or used.

myCode:

const squareList = arr => {
  // Only change code below this line

  return arr.filter(num => num > 0 && num % parseInt(num) === 0)
                        
                        .map(num => num * num);

  // Only change code above this line
};

const squaredIntegers = squareList([4, 5.6, -9.8, 3.14, 42, 6, 8.34, -2]);
console.log(squaredIntegers);

.filter() numbers that are not greater than zero && numbers that not do equal zero when finding the remainder bewtween a num and a num that has been passed through parseInt().

`.map()` through the array and take a `num` in its place and times it by itself.
1 Like

You can post solutions that invite discussion (like asking how the solution works, or asking about certain parts of the solution). But please don’t just post your solution for the sake of sharing it.

using filter() and map() and Number.isInteger() method.
here is my code :
const squareList = arr => {
// Only change code below this line
return arr
.filter(item => item>0 && Number.isInteger(item)==true)
.map(item => Math.pow(item,2) )

return arr;
// Only change code above this line
};

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