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


How can I retrieve the numbers which pass the test if they are integers? I cannot use a for loop. I tried to used map, but I always get false beacuse I compare numbers to booleans. I used // to mark it.

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

let positiveArr=arr.filter(numbers=>numbers>0)
         //console.log(filteredArr)
//let integerArr=positiveArr.filter(integers=>integers==)
let mappedArr=filteredArr.map(integers=>Number.isInteger(integers))
let newArr=[];
             
             

console.log(mappedArr)
  return arr;
  // Only change code above this line
};

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

Why not filter on both if the number is positive and isInteger, then map and do the multiplication.

1 Like

Because I get an empty arr when I do the comparation.

I was doing it wrongly. Thanks!

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

let positiveArr=arr.filter(numbers=>numbers>0)
         //console.log(filteredArr)
let integerArr=positiveArr.filter(integers=>Number.isInteger(integers))
//let mappedArr=filteredArr.map(integers=>Number.isInteger(integers))
let newArr=[];
             
           

console.log(integerArr)
  return arr;
  // Only change code above this line
};

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

I am thankful to you for your time.

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

let positiveArr=arr.filter(numbers=>numbers>0)
         //console.log(filteredArr)
let integerArr=positiveArr.filter(integers=>Number.isInteger(integers))
//let mappedArr=filteredArr.map(integers=>Number.isInteger(integers))
let newArr=integerArr.map
(squareNumber=>squareNumber*squareNumber)
             
           

console.log(newArr)
  return newArr;
  // Only change code above this line
};

const squaredIntegers = squareList([-3.7, -5, 3, 10, 12.5, 7, -4.5, -17, 0.3]);
//console.log(squaredIntegers);

now try using filter only once, you can combine the two conditions

1 Like

I will try that. Thanks!