Tell us what’s happening:
Hello guys,
I am really stuck with this lesson and have no idea hot to write the code for hours.
Please if anyone can help me and explain me detaily I would appreciate it.
Zoran
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;
// 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/68.0.3440.106 Safari/537.36
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/es6/write-higher-order-arrow-functions
Here is how I would approach this challenge.
I would filter the array by integers first then square them.
Hint: Use filter then map methods. You can search their usages on google especially on MDN.
1 Like
My approach:
The first part of the code should be to filter unwanted values with filter()
which will be used to both loop over the array and filter values, I’d prefer to use the following methods:
-
Number.isInteger()
to be able to test whether the value passed is an integer, not a string, not a float or anything else.
-
Logical operator
to be able to check whether the value is positive: value > 0
- Store the result in a variable to be used with .map()
The second part is to count the square of the filtered values, it’s possible to put the code all in .filter()
, but for readability, .map()
can also be used on top of filter for this operation:
- Use map() to loop over the filtered array and count the result of
value * value
.
Good luck.
What does it mean “square them”, there is no explanation for that?
This is what the instructions say.
Use arrow function syntax to compute the square of only the positive integers (fractions are not integers) in the array realNumberArray and store the new array in the variable squaredIntegers.
It means double or ^2