Functional Programming: Use Higher-Order Functions map, filter, or reduce to Solve a Complex Problem

Tell us what’s happening:
The code doesn’t work.
I’ve tried:

const squareList = arr =>
arr
.filter(num => num > 0 && num % parseInt(num) === 0)
.map(num => Math.pow(num, 2));
and I’ve tried:
const squareList = arr => {
return arr.reduce((sqrIntegers, num) => {
return Number.isInteger(num) && num > 0
? sqrIntegers.concat(num * num)
: sqrIntegers;
}, );
But nothing work’s
`squareList([4, 5.6, -9.8, 3.14, 42, 6, 8.34, -2])` should return `[16, 1764, 36]` .

`squareList([-3.7, -5, 3, 10, 12.5, 7, -4.5, -17, 0.3])` should return `[9, 100, 49]` .;Your code so far*

``````
const squareList = (arr) => {
// Only change code below this line
const squareList = (arr) => {
const positiveIntegers = arr.filter(num => {
return num >= 0 & Number.isInteger(num);
});
const squareList = positiveIntegers.map(num => {
return num ** 2;
});
return squaredIntegers;
};
// Only change code above this line
};

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

``````

User Agent is: `Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36`.

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

Don’t overthink it. The way I see this problem is you need to

1. Filter out non-integers
2. Filter out non-positives
3. Square each remaining value

So you’ll need three corresponding functions to carry out those tasks, and then pass those functions to the appropriate higher-order functions in a chain in that order.

As for choosing between filter, map, and reduce, I generally follow these guidelines.

• filter - when you want to remove elements from an array based on a condition
• Array => Array
• The new array’s length is less than or equal to the old array’s length
• Elements may be removed, but not changed
• Ex. “Remove all numbers less than 10”
• `arr.filter(x => x >= 10)`
• map - when you want to transform each element in an array uniformly
• Array => Array
• The new array’s length is equal to the old array’s length
• Elements may be changed
• Ex. “Add 5 to every number”
• `arr.map(x => x + 5)`
• Ex. “Convert all elements to strings”
• `arr.map(x => String(x))`
• reduce - when you want to transform (‘reduce’) an array into a single value
• Array => any
• Result can be of any type, even another array
• Ex. “Calculate the sum of all numbers in the array (Array => Number)”
• `arr.reduce((sum, x) => sum + x)`

If you need more help I’ve made a template of sorts below.

``````const isInteger = x => // true if x is an integer
const isPositive = x => // true if x is positive
const square = x => // square of x

return arr
./* filter | map | reduce */(isInteger)
./* filter | map | reduce */(isPositive)
./* filter | map | reduce */(square);
``````
2 Likes

Thank you but that still doesn’t work. I’ve tried different browser and nothing. I don’t see what I’m doing wrong. Probably some bug on their end.

here is the solution:
const squareList = arr

.filter(num => num > 0 && num % parseInt(num) === 0)

.map(num => Math.pow(num, 2));

{

``````return arr.reduce((sqrIntegers, num) => {

return Number.isInteger(num) && num > 0

? sqrIntegers.concat(num * num)

: sqrIntegers;

}, []);
``````

};

I am having the same issue. I believe the test for this challenge has a bug. I have tried every solution provided both in the hints and this thread. The console output is correct in each variation but the test that checks for the map(), filter(), and reduce() functions do not find them. I’m new to these forums. Am I allowed to screenshot the challenge and my code with the test results?

Don’t use screensnaps. Click on the ‘Get Help’ button and then ‘Ask for help’.

1 Like

Actually, I found an open issue on GitHub. The third test does not like line breaks within the chained function calls on this challenge. Remove the line breaks and the test passes. Mine just did, anyways.