Use Higher-Order Functions map, filter, or reduce to Solve

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] .

The test says I am getting this problem wrong by not returning an array, but I checked many times and the console looks like I’m returning an array.

const squareList = arr => {
  // Only change code below this line
  const square = arr.filter(num => {
    if(Number.isInteger(num) && Math.sign(num) === 1)
  return num})
  .map(num =>{
    return num * num
  })
  console.log(square)
  // Only change code above this line
};

const squaredIntegers = squareList([-3.7, -5, 3, 10, 12.5, 7, -4.5, -17, 0.3]);

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.

You can also 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 (’).

Logging is not the same as returning

1 Like

OMG, totally did not realize that i never returned it. Thanks!

1 Like

No problem. We’ve all done that one!