I tried to write a arrow function to compute the square of only the positive integers ( not include the fractions).
const realNumberArray = [4, 5.6, -9.8, 3.14, 42, 6, 8.34];
const squareList = (arr) => {
"use strict";
const squaredIntegers = (arr) => {
let arrayChoosen = arr.filter(ele => ele > 0 && Number.isInteger(ele));
return arrayChoosen.map(x => x * x);
}
return squaredIntegers;
};
// test your code
const squaredIntegers = squareList(realNumberArray);
console.log(squaredIntegers);
But the result is a function declaration, not a array as I expect. But when I try to modify the code in this way
const squareList = (arr) => {
"use strict";
let squaredIntegers = arr.filter(ele => ele > 0 && Number.isInteger(ele));
squaredIntegers = squaredIntegers.map(val => val * val);
return squaredIntegers;
};
Then it output the array I expect. Why in the first case it doesn't work?
squaredIntegers()is never called, but only the function reference is returned. So why would you expect the result to be an array in the first place?