Write Higher Order Arrow Functions1

Write Higher Order Arrow Functions1
0

#1

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


#2

@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.


#3

It is also possible to solve this challenge with a single reduce which would only iterate through the array a single time instead of iterating through the array with filter first and then a smaller version of the array with map.

In general, reduce can solve any problem where a map/filter combination exists.

@Klaudia You did not specifically say what you needed help with. In the future, give us more details of what help you need. Do you not understand the directions? Are you having trouble developing an algorithm? Are you having trouble coding your developed algorithm? This is the kind of information we need every time you ask for help on the forum. Otherwise, we may be giving you information that you did not need or want at this time.


#4

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


#5

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


#6

Your function must return the array [16, 1764, 36] after receiving the array [4, 5.6, -9.8, 3.14, 42, 6, 8.34]. Your function must isolate and square only the integers (numbers without decimals) and put those squared numbers into a new array referenced by squaredIntegers. The return statement is already provided for you, so you do not have to write that.


#7

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

squaredIntegers([16, 1764, 36])

Like @RandellDawson 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]


#8

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


#9

The tests are going to call the squareList function. squaredIntegers is the variable you need to assign the created array to. You still have to write code which will create the array and put the correct values (the squared integers) into it.


#10

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


#11

If the instructions were create a function (named it anything you want) that will take an array as an argument . This array will contain numbers (integers and fractions). Your function must return a new array with ONLY the integers of the array passed into it.

If the above was the instructions instead, how would you write that function? For example, if the array you were given was [4, 5.6, -9.8, 3.14, 42, 6, 8.34], your function would need to return [4, 42, 6], because those are the only integers in the array. Try writing the function I describe above instead using just the filter function.


#12

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.


#13

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


#14

See those two comments. Do exactly what they say. Do not modify any other existing code in the challenge.

Honestly, you seem to not understand some basic concepts about how functions work. I strongly suggest you start at the first lesson again in the Basic JavaScript section before continuing any further.


#15

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?


#16
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.


#17

I do it and it is right


#18

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


#19

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?


#20

You need to write the inner part of the squareList function assuming you do not know what the array looks like before it is passed into the function. This means that you would not be able to know in advance what the final array should look like and therefore you would not be able to hard-code the array as you have in the line below.

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