Write Higher Order Arrow Functions /solved it but still have a question

Write Higher Order Arrow Functions /solved it but still have a question
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 positive =realNumberArray.filter(realNumberArray=>
 realNumberArray>0 && realNumberArray % 1==0);
  const squaredIntegers = positive.map(x=>x*x);
  
  
  
  // 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.1) 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

So I solved it like this, but code seems messy and I don’t understand what const squareList = (arr) => { at start of the code means. why do they even need arr variable initially when I ended up not using it at all??
Can anyone tidy up my code so that it will look nicer and cleaner?? Thanks!!


#3

Your squareList function takes an array as a parameter and transforms this array as an array containing the squared integers.

const squareList = (arr) => { ... } means : create a function called square that takes an array as a parameter

You’ve done a little mistake. It shouldn’t be realNumberArray but arr.
In fact, the goal of that function is to be reusable.

Here is a cleaner version :

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(number => number > 0 && number % 1 === 0)
    .map(number => number*number);
  // change code above this line
  return squaredIntegers;
};
// test your code
const squaredIntegers = squareList(realNumberArray);

I just replaced realNumberArray with arr and chained the array methods.


#4

THank you so much!!
I didn’t know that I can combine those two filter and map arrow functions with dot as well.
Btw, in the last line,
const squrardIntegers = squareList(realNumberArray);
I don’t quite get this. Can we just test the function like
squareList (realNumberArray); ?
since it will return squaredIntegers.
Also, I don’t understand where number came from. Shouldn’t it be array to be reusable? or are you making new parameter for the sake of reusabiility?
So either number or x in this line below,

const squaredIntegers = arr.filter(number => number > 0 && number % 1 === 0)
    .map(number => number*number);

are we making new local parameter inside of squaredIntegers function? I found it it works when I put x or y instead of number.
thanks in advance.


#5

The above line assigns the value returned from calling the squareList function with realNumberArrayto the variable named squaredIntegers.

The number argument is just a name which represents the value of the element being iterated over by the filter method. You can name it what ever you want, but it is best to name it something which describes what it contains to make your code more readable. Since arr is an array of numbers, it makes since to refer to each element as number instead x.