We have defined a function named squareList
. You need to complete the code for the squareList
function using any combination of map()
, filter()
, and reduce()
so that it returns a new array containing only the square of only the positive integers (decimal numbers are not integers) when an array of real numbers is passed to it. An example of an array containing only real numbers is [-3, 4.8, 5, 3, -3.2]
.
The test says I am getting this problem wrong by not returning an array, but I checked many times and the console looks like I’m returning an array.
const squareList = arr => {
// Only change code below this line
const square = arr.filter(num => {
if(Number.isInteger(num) && Math.sign(num) === 1)
return num})
.map(num =>{
return num * num
})
console.log(square)
// Only change code above this line
};
const squaredIntegers = squareList([-3.7, -5, 3, 10, 12.5, 7, -4.5, -17, 0.3]);