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.
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().