Boris Aronson
Boris Aronson

Reputation: 207

Reduce vs Filter and Map

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

Answers (8)

d-h-e
d-h-e

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

jo_va
jo_va

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

Dacre Denny
Dacre Denny

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:

  1. the input value being iterated is "filtered" if it is a whole numbers via Number.isInteger(number). If the number i is an integer then the code
  2. proceeds to concatenate the "square" of that number to the output array a via this a.concat([ i ** 2 ]) (note that number ** 2 is shorthand for Math(number, 2)
  3. if i is not an integer, then it's filtered out by directly returning the current output array a for that iteration

Hope that helps!

Upvotes: 1

prasanth
prasanth

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

Nina Scholz
Nina Scholz

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

Ankit Agarwal
Ankit Agarwal

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

ellipsis
ellipsis

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

Snow
Snow

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

Related Questions