# Use Higher-Order Functions map, filter, or reduce to Solve a Complex Problem

``````const squareList = arr => {
// Only change code below this line

// Use filter to find the positive integers (No negative numbers or decimals)

// Use reduce to mutiply the positive numbers by their own value (example: answer should be [25, 9])

// return the new array
return arr;
// Only change code above this line
};

const squaredIntegers = squareList([-3, 4.8, 5, 3, -3.2]);
console.log(squaredIntegers);
``````

In my own words, I would say if a value in the array is less than 0â€¦then it is not positive. And if a value doesnâ€™t have a remainder of 0â€¦then it is not an integer but instead a decimal. Saying it in my own words seems simple but writing it as code is a different story.

Ahhhh so I should use the map method to mutiply the remaining numbers in the array by their own value. Something like:

``````[ ].map(squareList(i) i * i);
``````
``````const squareList = arr => {
// Only change code below this line

// Step 1: Use filter to find the positive integers (No negative numbers or decimals)

// Step 2: Use map to mutiply the positive numbers by their own value (example: answer should be [25, 9])

return arr;
// Only change code above this line
};

const squaredIntegers = squareList([-3, 4.8, 5, 3, -3.2]);
console.log(squaredIntegers);
``````

When I am confused I write down the steps I think need to happen as if I were explaining it to a kid. I have done that but I am stuck when it comes to actually applying methods I have learned in previous lessons. Can someone help me out without giving me the answer?

You have something for this part, but it has a small issue

This syntax doesnâ€™t look like what you use inside of the map method.

Can you write a function that takes a number and returns itâ€™s square? That function should be the argument for your map

Would it be something like this?

``````.map(num => num * num);
``````
``````const squareList = arr => {
// Only change code below this line
arr.filter(num => num > 0 && num //(num must have a remainder of 0 which indicates it is not a decimal))
arr.map(num => num * num);
return arr;
// Only change code above this line
};

const squaredIntegers = squareList([-3, 4.8, 5, 3, -3.2]);
console.log(squaredIntegers);
``````

This is what I have now but I know I am doing something wrong

Calling map or filter doesnâ€™t change `arr`. That makes a brand new array.

Ohhhh okay, so instead I should keep it how I had it the first time and indicate that these changes are going into a brand new array. Like this? :

``````const squareList = arr => {
// Only change code below this line
[].filter(num => num > 0 && num //(num must have a remainder of 0 which indicates it is not a decimal))
[].map(num => num * num);
return arr;
// Only change code above this line
};

const squaredIntegers = squareList([-3, 4.8, 5, 3, -3.2]);
console.log(squaredIntegers);
``````

I am having trouble remembering how I can indicate that the numbers must not be decimals. I believe the % sign is for remainders but I donâ€™t know how to go about using it for the second half of the filter syntax

No.

This line right here is fine, but its making a brand new array instead of changing `arr`. You need to use the return value instead of throwing it away.

See the example here:

``````const squareList = arr => {
// Only change code below this line
arr = [].filter(num => num > 0 && num //(num must have a remainder of 0 which indicates it is not a decimal))
arr = [].map(num => num * num);
return arr;
// Only change code above this line
};

const squaredIntegers = squareList([-3, 4.8, 5, 3, -3.2]);
console.log(squaredIntegers);
``````

Like this?

Does that look like the example I linked to?

``````const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];

const result = words.filter(word => word.length > 6);

console.log(result);
``````

It doesnâ€™t look like that example to my eye

``````const squareList = arr => {
// Only change code below this line
squaredIntegers = arr.filter(num => num > 0 && num //(num must have a remainder of 0 which indicates it is not a decimal))
squaredIntegers = arr.map(num => num * num);
return arr;
// Only change code above this line
};

const squaredIntegers = squareList([-3, 4.8, 5, 3, -3.2]);
console.log(squaredIntegers);
``````

I tried doing this first to make it look just like the example, but this gives me an error and says TypeError: â€śsquaredIntegersâ€ť is read-only

You declared `squaredIntegers` outside of the function as a `const`. Thatâ€™s confusing. I would use a different function name inside of your function

Ahh I see, so I should use result as my function name?

Huh? I donâ€™t know what â€śresult as your function nameâ€ť would mean. The function name is given to you:

``````const squareList = arr => {
// Only change code below this line
squareList = arr.filter(num => num > 0 && num) //(num must have a remainder of 0 which indicates it is not a decimal))
squareList = arr.map(num => num * num);
return arr;
// Only change code above this line
};

const squaredIntegers = squareList([-3, 4.8, 5, 3, -3.2]);
console.log(squaredIntegers);
``````

So it should be this?

No.

You seem to be confused about how variables work?

Do you know how to declare variables?

`arr` will not be changed by calling `map` or `filter`, so you should not return `arr`.

You need to declare a new variable that doesnâ€™t share any name with something else to hold the result here.

Yes you can declare variables using var, let, and const. People donâ€™t really use var a lot because it causes a lot of problems because it can be easily changed. Using const keeps whatever youâ€™re declaring constant. So in this case I can use let like:

``````let positiveIntegersSquared = arr.filter(num => num > 0 && num) //(num must have a remainder of 0 which indicates it is not a decimal))
``````

Sorry for being so slow, I know itâ€™s probably frustrating

This is closer. You havenâ€™t squared anything yet, but this does get you positive numbers

Okay I think this is looking a lot better, I am just having trouble indicating that the number must be an integer and not a decimal. Iâ€™m sure thereâ€™s multiple ways to do this but I was trying to find a way to say if the number has a remainder of 0 because then the computer should know that number is an integer

``````const squareList = arr => {
// Only change code below this line
let positiveIntegersSquared = arr.filter(num => {
if (num > 0 && num) return num; //(num must have a remainder of 0 which indicates it is not a decimal))
}).map(num => { return num * num });