Reputation: 207
I've written a function that computes the square of only the positive integers in the array realNumberArray and returns a new array with the result.
example: [4, 5.6, -9.8, 3.14, 42, 6, 8.34, -2] returns [16,1764,36]
How would you recreate the following function using only reduce() and what would be the preferred way?
const realNumberArray = [4, 5.6, -9.8, 3.14, 42, 6, 8.34, -2];
const squareList = (arr) => {
const squaredIntegers = arr;
let newArr=squaredIntegers.filter(val=>val%1==0 && val>0).map(val=>Math.pow(val,2))
return newArr;
};
const squaredIntegers = squareList(realNumberArray);
console.log(squaredIntegers);
Upvotes: 5
Views: 10419
Reputation: 2548
Using reduce and Array destructuring instead of push method
const realNumberArray = [4, 5.6, -9.8, 3.14, 42, 6, 8.34, -2];
const newArray = realNumberArray.reduce((acc, curr) => (curr % 1 === 0 && curr >= 0 ? [...acc, curr * curr] : acc), []);
console.log(newArray);
Upvotes: 0
Reputation: 13964
You can do it like this with reduce:
const squareList = arr =>
arr.reduce((out, x) => x % 1 === 0 && x > 0 ? [...out, x * x] : out, []);
console.log(squareList([4, 5.6, -9.8, 3.14, 42, 6, 8.34, -2]));
Upvotes: 2
Reputation: 30360
You could achieve this via the following:
const realNumberArray = [4, 5.6, -9.8, 3.14, 42, 6, 8.34, -2];
const result = realNumberArray.reduce((a, i) => Number.isInteger(i) && i > 0 ? a.concat([ i ** 2 ]) : a, [])
console.log(result)
To break this down, the code above reduces the input array realNumberArray
to the result
array where for each reduce iteration:
Number.isInteger(number)
. If the number i
is an integer then the codea
via this a.concat([ i ** 2 ])
(note that number ** 2
is shorthand for Math(number, 2)
i
is not an integer, then it's filtered out by directly returning the current output array a
for that iterationHope that helps!
Upvotes: 1
Reputation: 22490
Try this
const realNumberArray = [4, 5.6, -9.8, 3.14, 42, 6, 8.34, -2];
const squareList = (arr) => {
return arr.reduce((a,val)=>{
if(val%1==0 && val>0)
a.push(Math.pow(val,2))
return a;
},[])
};
const squaredIntegers = squareList(realNumberArray);
console.log(squaredIntegers);
Upvotes: 1
Reputation: 386550
You could concat an empty array of the sqared value, depending on the not integer value or if negative.
const
realNumberArray = [4, 5.6, -9.8, 3.14, 42, 6, 8.34, -2],
squareList = array => array.reduce((r, v) => r.concat(v % 1 || v < 0 ? [] : v * v), []),
squaredIntegers = squareList(realNumberArray);
console.log(squaredIntegers);
Upvotes: 0
Reputation: 30739
The best way is to check if parseFloat()
and parseInt()
on that number is same or not to figure out that the given number is pure integer. Then, based on your additional condition where the number should be greater than 0, add extra condition for that in the if
.
var arr = [4, 5.6, -9.8, 3.14, 42, 6, 8.34, -2] ;
var res = arr.reduce((acc, num) => {
if(num > 0 && parseFloat(num) === parseInt(num)){
acc.push(num*num);
}
return acc;
}, []);
console.log(res);
Upvotes: 0
Reputation: 12152
Using reduce
const realNumberArray = [4, 5.6, -9.8, 3.14, 42, 6, 8.34, -2];
const squareList = (arr) => {
const squaredIntegers = arr;
let newArr = squaredIntegers.reduce((acc, val) => {
if (val % 1 == 0 && val > 0) {
acc.push(Math.pow(val, 2));
return acc;
}
return acc
}, [])
return newArr;
};
const squaredIntegers = squareList(realNumberArray);
console.log(squaredIntegers);
Upvotes: 0
Reputation: 4097
Push to the accumulator only if the test passes:
const realNumberArray = [4, 5.6, -9.8, 3.14, 42, 6, 8.34, -2];
const squareList = arr => arr.reduce((a, val) => {
if (val % 1 == 0 && val > 0) {
a.push(val ** 2);
}
return a;
}, []);
const squaredIntegers = squareList(realNumberArray);
console.log(squaredIntegers);
Upvotes: 1