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

The Challenge is
We have defined a function named squareList . You need to complete the code for the squareList function using any combination of map() , filter() , and reduce() so that it returns a new array containing only the square of only the positive integers (decimal numbers are not integers) when an array of real numbers is passed to it. An example of an array containing only real numbers is [-3, 4.8, 5, 3, -3.2] .

Note: Your function should not use any kind of for or while loops or the forEach() function.

So far am able to come up with this solution

const squareList = (arr) => {

  // Only change code below this line

  return arr.filter(mum => mum > 0)

  // Only change code above this line

};

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

console.log(squaredIntegers); // output [4.8,  5,  3]

It won’t filter properly because of the decimal number that is still in the array. Can any help me out?

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

Please use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks are not single quotes.

markdown_Forums

at this time the only condition in your code is the callback, which is mum => mum > 0, and this is filtering out negative numbers

to have only whole integers, you need to add a second condition.

Do you think you can google a bit and see what you find?
There are many different ways for this

I have tried Google, i couldn’t find any helpful hint. I also checked freeCodeCamp’s guide for the answer but i don’t seem to understand the solution.

Thanks, i will take note of that next time

you need to check if the number is a whole number or have decimals

If I google “javascript test whole number” there are various interesting things to read. Also, just scrolling through the results, the third result is a method called isInteger().

Check if that can be useful.

Finally i got it, now i fully comprehend how the whole magic went…

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

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

Thanks alot, i really appreciate.