 # Write Higher Order Arrow Functions: .map query

Write Higher Order Arrow Functions: .map query
0

#1

Tell us what’s happening:
I need to include .map() function. I imagine that this needs to occur outside the filter function (and not nested within) but this doesn’t seem to work.

So, my question is two:

1. Do I need to include a separate .map function outside the parentheses or is it okay to next them?
2. Am I using % to focus map on non-decimal numerals?

Thank you
Nick

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;
arr.filter((squareList) => realNumberArray > 0);
// change code above this line
return squaredIntegers;
};
// test your code
const squaredIntegers = squareList(realNumberArray);
console.log(squaredIntegers);
``````

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

#2

First thing I want to point out.

When you type arr.filter(blah blah blah), you return a filtered array. As it is right now, your code does nothing with that returned, filtered array.

#3

To kind of elaborate on zapcannon99’s point, just as we chain String methods (like myString.trim().split(""), for example), we can chain array methods (to continue with the above since the split turned myString toan array, myString.sort(…).join("-") would take that sorted array and perform a join on that.

Logically, can you see an application to your question?

As to your decimal question, how about checking if the given number is equal to itself converted to an integer?

Also, take a look at your filter function. It is not going to do what you expect. Isuggest you look at the filter() docs. The callback in it defines a param that the filter will assin to each member of your arr as it iterates through it, so you create a locally scoped squareList and then never use it.

Your func is passed an array, arr. You can’t rely on using realNumbersArray, as that may not always be your passed parameter…

#4

You’ll gonna need:

1. Number.isInteger() which determine if number is whole.
2. After that numbers that are bigger then zero.
3. Previous two are achievable by using .filter()
4. After getting filtered array, they you use .reduce()
5. Product of reduce is what you need to return.

#5

I’ve tried a few these suggestions out. I use Number.isInteger(arr) and it doesn’t seem to make that much of a difference. I don’t know if I’m using it correctly. I also used parseInt(arr) which didn’t seem to make a difference. I chained .reduce(arr) to filer and it returned an error. When not returning errors, my code does not return only squares of integers. So I’m not sure if I’m wrong for continuing to use arr or is I should be using a different math function.
Should I even be filtering squareList? I don’t know what else I would use, but I’m wondering if this one of the problems with my code.

``````
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;
arr.filter((squareList) => Number.isInteger(arr) && arr>0);
// change code above this line
return squaredIntegers;
};
// test your code
const squaredIntegers = squareList(realNumberArray);
console.log(squaredIntegers);

``````

#6

Let’s look at what you latest code actually does.

``````const squareList = (arr) => {
"use strict";
// change code below this line
const squaredIntegers = arr;
arr.filter((squareList) => Number.isInteger(arr) && arr>0);
// change code above this line
return squaredIntegers;
};
``````

The line below creates another reference to arr. This does not make a copy of arr and assign it to squaredIntegers. Any additions/deletions to squaredIntegers will be affect arr, because they are referencing the same array in memory.

``````const squaredIntegers = arr;
``````

The line below does create a new array, but it is an empty array, because you test if arr is a number (which it is not, because it is an array) and if arr > 0 (why would you compare an array with zero?). Also, even if you would have created the correct filter logic, you do nothing with the returned array (i.e. assign it to a variable), so nothing would have really happened to help you.

`````` arr.filter((squareList) => Number.isInteger(arr) && arr>0);
``````

Finally, you return squaredIntegers, but since squaredIntegers refers to the same array that arr does, you are just returning the same (unmodified) array back to the calling function.

So even if you figure out how to correctly filter only positive integers, you solution would still be missing certain logic. You are not just supposed to return an array of positive integers. Instead, you are supposed to return the squared value of those positive integers. Think how you could use the map method on this filtered array.

#7

``````const squaredIntegers =  arr.filter((x) => Number.isInteger(x)==true && x>0);
``````

As Randal said and i would like to add, look between these two examples and try to understand the difference.

#8

Thanks for the example code. That actually put into perspective what was being done. I’m still at an impasse in regard to how to return the squared integers. I have attempted to do so using map. My effort is below. Where am I going wrong?

``````
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((x) => Number.isInteger(x) == true && x>0);
arr.map(x => x*x);

// change code above this line
return squaredIntegers;
};
// test your code
const squaredIntegers = squareList(realNumberArray);
console.log(squaredIntegers);

``````

#9

My bad, I was thinking this was the summing one.

But again, arr.map RETURNS a value, and you’re not doing anything with that. In the arr.filter function, the returned value is being stored into squaredIntegers. However, those returned values aren’t what you’re squaring – you’ve gone back to the original array. And squared everything. And not caught the returned value.

#10

Int the code above, you have successfully created a new array referenced by squareIntegers which contains only positive integers. Now you need to use map on this newly created array which will actually create another new array of the squared values. Instead, you are using map on the original arr passed into the squareList function. Remember, arr will contain numbers which are not positive and not integers. Also, just applying the map to an array in itself does not do anything, you would either need to return the mapped array or assign it to another variable which would get returned later.

In this case, the challenge wants the final array returned to be named squaredIntegers, so you could simply add .map on the end of the filter, after the closing parentheses but make sure to remove the semi-colon.

#11

That did it. Thank you.