Search code examples
javascriptarrow-functions

Write Higher Order Arrow Functions


I'm doing FreeCodeCamp tasks, and now I get stuck with Arrow Function Problem is : I need to sort an array(which "filter" function doing well - it sorts) but my map. function doesnt work. I get an error "((num > 0) && Number.isInteger(...)).map is not a function"

Thanks in advance

const realNumberArray = [4, 5.6, -9.8, 3.14, 42, 6, 8.34, -2];
const squareList = (arr) => {
  "use strict";
  const squaredIntegers = arr.filter((num) =>
   (num > 0 && Number.isInteger(num)).map((num) => Math.pow(num,2) )) 
  return squaredIntegers;
};

It should return an array the square of only the positive integers.


Solution

  • Put the .map after the .filter is completely done:

    const realNumberArray = [4, 5.6, -9.8, 3.14, 42, 6, 8.34, -2];
    const squareList = (arr) => {
      return arr
        .filter(num => num > 0 && Number.isInteger(num))
        .map(num => num ** 2)
    };
    
    console.log(squareList(realNumberArray));