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

THE TASK Complete the code for the squareList function using any combination of map(), filter(), and reduce(). The function should return a new array containing the squares of only the positive integers.

I dont understand parseInt() role here. Why doesnt my code work when I leave out parseInt? parseInt just converts a string to a number, correct? If every element in the array is a number, then why is parseInt needed here?

const squareList = arr => {
  // Only change code below this line
  return arr
  .filter(num => num > 0 && num % parseInt(num) == 0)
  .map(num => Math.pow(num, 2));

  // Only change code above this line

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:107.0) Gecko/20100101 Firefox/107.0

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

Link to the challenge:

My first instinct was that I was going to look up the function parseInt to answer you because I noticed that your array contains decimals. But instead I’m giving you this homework. What does parseInt do with integers vs decimals? You look this up and let me know.

ps. second instinct is to find out how JS treats arrays that have a mix of ints and decimals.

edit: you should be asking what the % does when it has a decimal and the int version of that decimal also (eg. if 3.7 % 3, what is the return? you can write a small program to check)


That woman is a volunteer and an experienced professional programmer who is offering her free time and expertise to help you. Let’s give her a 10% raise. Let’s see, what is 10% of $0?

I agree with her - you will learn a lot more by looking them up yourself. One of the hardest things to convince learners is the importance of “looking things up” Google is a developer’s best friend. I can’t say that enough times.

Google “mdn parseint” and “mdn js operators”(to get info on %).

One of the things that drives me crazy on here sometimes is the people that don’t want to learn how to do this. Looking things up in the docs is how you learn this stuff.

If you look those and don’t understand how they work in combination, then please ask again, explaining what is not clear.


This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.