Getting an array of squares of only positive integers from the given array

I am just wondering why the second version of the code with the if statement doesn’t work, and what needs ro be changed in order for it to work? Thanks in advance.

Your code so far


const squareList = (arr) => {
return arr.reduce((sqArr, n) => {
  return Number.isInteger(n) && n > 0
  ? sqArr.concat(n*n)
  : sqArr
}, []);
};

const squareList = (arr) => {
return arr.reduce((sqArr, n) => {
  if(n > 0 && Number.isInteger(n)) sqArr.concat(n*n);
  else sqArr
}, []);
};

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36.

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

Link to the challenge:

Hello there,

The second version does not return anything in the reduce method. So, the parameters are not defined.

Hope this helps

Isn’s else returning the accumulator? What is the correct way of doing this with the if statement? Thank you for the resonse btw.

In a sense, I suppose it is. However, it is not returning it through the function scope; it is just returning it to the function scope (nothing outside has access).

const squareList = (arr) => {
  return arr.reduce((sqArr, n) => {
    if(n > 0 && Number.isInteger(n)) return sqArr.concat(n*n);
    else return sqArr;
  }, []);
};

Hope this helps