Write Higher Order Arrow Functions j

Write Higher Order Arrow Functions j
0

#1

Tell us what’s happening:

i dont understand this lesson and what is the function of the filter

Your code so far


const realNumberArray = [4, 5.6, -9.8, 3.14, 42, 6, 8.34];
const squareList = (arr) => {
  "use strict";
  // change code below this line
   const squaredIntegers = arr.filter((num) => Number.isInteger(num));
  
  squaredIntegers = squaredIntegers.map((square) => square*square);
  // 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 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/65.0.3325.181 Chrome/65.0.3325.181 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/es6/write-higher-order-arrow-functions


#2

The filter method allows you to create a new array with all elements that pass the test implemented by a supplied function. If the test returns true, then the element is kept in the new array. If it is false, the element is not kept in the new array. In your code above, the filter is returning the evaluation of Number.isInteger on each element in arr. Only integers will be kept in the new array which gets assigned to squaredIntegers.

For example, the following:

const arr = [4, 5.6, -9.8, 3.14, 42, 6, 8.34];

const squaredIntegers = arr.filter((num) => Number.isInteger(num));
console.log(squaredIntegers); // displays [ 4, 42, 6 ]

is similar to:

const arr = [4, 5.6, -9.8, 3.14, 42, 6, 8.34];

const newArr = [];
for (let i = 0; i < arr.length; i++) {
  if (Number.isInteger(arr[i])) {
    newArr.push(arr[i]);
  }
}
console.log(newArr); // displays [ 4, 42, 6 ]