Write Higher Order Arrow Functions1

Tell us what’s happening:

I want help!

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/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

@Klaudia First you’ll want to filter() positive integers (fractions are not integers). Then, once you have an array of these integers, you can use map() to square each integer and return the result.

Note: it is possible to chain functions like arr.filter().map();.

Does that help? I had trouble with this one when I first came across it. You can see my thoughts about it in this forum topic reply.

1 Like

Yes and thank you for your advice.In that exercise I don’t understand that:
squaredIntegers should be [16, 1764, 36]

How can I do the code because I try:
, squaredIntergers[16, 1764, 36];
but it isn’t right

squaredIntegers would be a function that would accept [16, 1764, 36] as an argument or parameter:

squaredIntegers([16, 1764, 36])

Like @camperextraordinaire said, you would need to pass [4, 5.6, -9.8, 3.14, 42, 6, 8.34] for you to get the expected answer [16, 1764, 36]

const realNumberArray = [4, 5.6, -9.8, 3.14, 42, 6, 8.34];
const squareList = (arr) => {
“use strict”;
// change code below this line
squaredIntegers([16, 1764, 36]);
// change code above this line
return squaredIntegers;
};
// test your code
const squaredIntegers = squareList(realNumberArray);
console.log(squaredIntegers);

I put that code but it is tell me:
squaredIntegers is not a function
squaredIntegers is not a function
squaredIntegers is not a function
squaredIntegers is not a function
squaredIntegers is not a function
squaredIntegers is not a function
squaredIntegers is not a function

Have you declared your squaredIntegers() function? When I look at your code sample above I don’t see where you declared a const or let variable which contains squaredIntegers nor a declared function

In fact, I see that squaredIntegers is being called before it is being declared:

const realNumberArray = [4, 5.6, -9.8, 3.14, 42, 6, 8.34];
 const squareList = (arr) => {
“use strict”;
 // change code below this line
 squaredIntegers([16, 1764, 36]); //<---Where it is called
 // change code above this line
 return squaredIntegers;
 };
 // test your code
 const squaredIntegers = squareList(realNumberArray); //<--- Where it is declared
 console.log(squaredIntegers);

Maybe a better route would be creating the logic to sort through which numbers are integers and squaring first.

Yes I do it:
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([16, 1764, 36]);
console.log(squaredIntegers);

But now I have wrong that:

map, filter, or reduce should be used

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 = ([16, 1764, 36]);
// change code above this line
return squaredIntegers;
};
// test your code
const squaredIntegers = squareList(realNumberArray);
console.log(squaredIntegers);

I do that and it is right now what to put this words?

const squaredIntegers = ([16, 1764, 36]);

This will not work. Essentially, the solution is being declared in your variable. This would need to equal the logic that needs to be performed.

I do it and it is right

Now I would put that:
map, filter, or reduce should be used
but I didn’t know where to put it

map, filter, or reduce should be used
but I didn’t know where to put it

However, do you understand why that is being used? Would you be able to explain what is happening through each of those array methods and why they are chained the way they are?

You explain me so nice but my brain now isn’t work and you can’t you explain me more easily:thinking::joy:

I have gto many hours which I try to do it!

OK thank you for helping .

cool. good tip
Thanks for the post

If anybody still struggles with this problem, this worked for me: first filter the positive integers, then map in order to find the squares.

const squaredIntegers = arr
.filter(num => (num > 0 && num % 2 == 0))
.map(num => num * num)

3 Likes

I understand the theory of how this excercise works but can’t figure out how the syntax works. I have used the filter function to loop through the realNumbersArray to find whole numbers. How can I use the maps function to square the result? I
Any help would be much appreciated :slight_smile:

const realNumberArray = [4, 5.6, -9.8, 3.14, 42, 6, 8.34, -2];
const squareList = (arr) => {
  "use strict";
  // change code below this line
  const squaredIntegers = arr.filter(num => {
    return  num % 1 === 0;
    //how do I incorperate the map() function?
  });
  // change code above this line
  return squaredIntegers;
};
// test your code
const squaredIntegers = squareList(realNumberArray);
console.log(squaredIntegers);