if block is executing properly. What is wrong with the code?
let arr = [4, -4, 5.8, 1.6];
let newA = arr.filter((x) => {
if (x > 0 && Number.isInteger(x)) {
return x * x;
}
})
console.log(newA);
Expected output
[16]
Actual output:
[4]
why?
Filtershould return true/false result, what you're looking is probablymap.filterexpects the predicate to return a boolean (true/false), and you returnx * xwhich evaluates to for example16for your first element which is atruthyvalue i.e. is not filtered from your result set.