Please explain filter function

Please explain filter function
0.0 0

#1

Tell us what’s happening:

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;
  squaredIntegers.filter((arr)=> {
    if(arr>0 && arr % 1 != 0)
      return arr*arr

  })
  // 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 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36.

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


#3

The function passed into filter returns a true or false for each item, your function here looks like something you might use for ‘map’ (which allows you to perform a function on each item then return it).

So where you have return arr*arr, you should maybe filter out the invalid values first and then do map or a loop to create the new array of squared values from the filtered one.


#4

It took me a while to understand this problem also. Watching this video helped me solve the problem. https://tylermcginnis.com/arrow-functions/

Ask yourself, "how can I reduce this code to 1 line using arrow functions?

Hint: filter() and map()