Tell us what’s happening:
The code doesn’t work.
I’ve tried:
const squareList = arr =>
arr
.filter(num => num > 0 && num % parseInt(num) === 0)
.map(num => Math.pow(num, 2));
and I’ve tried:
const squareList = arr => {
return arr.reduce((sqrIntegers, num) => {
return Number.isInteger(num) && num > 0
? sqrIntegers.concat(num * num)
: sqrIntegers;
}, );
But nothing work’s
squareList([4, 5.6, -9.8, 3.14, 42, 6, 8.34, -2])
should return [16, 1764, 36]
.
squareList([-3.7, -5, 3, 10, 12.5, 7, -4.5, -17, 0.3])
should return [9, 100, 49]
.;Your code so far*
const squareList = (arr) => {
// Only change code below this line
const squareList = (arr) => {
const positiveIntegers = arr.filter(num => {
return num >= 0 & Number.isInteger(num);
});
const squareList = positiveIntegers.map(num => {
return num ** 2;
});
return squaredIntegers;
};
// 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 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36
.
Challenge: Use Higher-Order Functions map, filter, or reduce to Solve a Complex Problem
Link to the challenge: