Filter array Arrow Functions get element

Tell us what’s happening:

How can i get the single array element in filter with arrow funtions?

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(()=> return ..........);
  // 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/67.0.3396.99 Safari/537.36.

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

The single array elements are passed as the first argument to the arrow function.
arr.filter((element) => ....).
In addition, be aware that if you do not use {} for the arrow function, the expression
you supply gets returned implicitly. In the code you showed, you do not use them and
therefore do not need the return keyword.

Arrow functions are similar to regular functions in almost everyway. You just have to change the syntax for function declaration:

So , for eg.

function add (a, b) {
    return a + b; 
}

The same thing you can write in arrow function systax as:

const add = (a, b) => {
     return a + b
};

To make it more easier arrow functions has a some shortcuts:

  1. If you have only one parameter you can ignore the brackets:
const square = a => {
     return a * a;
};
  1. If you have only one statement inside the function block you can return directly without using the return keyword:

const square = a => { a * a };
or simply

const square = a => a * a;

So in your case it would be as:

const squaredIntegers = arr.filter((element) => Number.isInteger(element) && element > 0 );

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(w=>w>0&&Number.isInteger(w));
// change code above this line
return squaredIntegers.map(w=>Math.pow(w,2));
};
// test your code
const squaredIntegers = squareList(realNumberArray);
console.log(squaredIntegers);

check this

first filter positive and integers numbers then map them with there squares

filter first argument is a callback function,

the first argument of this callback is the element looked upon.

So in code:

// a function that log its argument
function exampleFilter(element) { 
 console.log(element)
 return true;
}

// that function can be passed to filter as first arg
arr.filter(exampleFilter) // will log each element of the array, but won't actually filter since it's returning true

As you know then, arrow function can be used instead of a normal function so exampleFilter can be writtnen as

 const exampleFilter = (element) => {
 console.log(element) 
 return true
}

// or writing inline
arr.filter(element => {
console.log(element)
return true;
})

Hope it gives you a good start :slight_smile: