Tell us what’s happening:
I’d struggled to pass this challenge but please can someone explain it better for me, because barely understand it. thanks in advance.
Your code so far
const realNumberArray = [4, 5.6, -9.8, 3.14, 42, 6, 8.34, -2];
const squareList = (arr) => {
"use strict";
// change code below this line
const squaredIntegers = arr.filter(num => Number.isInteger(num) && num > 0).map(x => x * x);
// change code above this line
return squaredIntegers;
};
// test your code
const squaredIntegers = squareList(realNumberArray);
console.log(squaredIntegers);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36.
With the filter method you create an array with only positive integer numbers, with map you then square them
Methods like filter and map accept a callback function. Filter accept a callback function, and if the callback function returns true the element is included in the new array, of it returns false than it is excluded. Map also accept a callback function, and the callback function returns the new value of the element. You will see map, filter and other higher order function in future challenges, so don’t worry too much over them, focus on the arrow notation for now, you will find it useful later on.