user9685772
user9685772

Reputation:

Javascript Filter integers

I'm learning javascript on FreecodeCamp. And, the solution of my problem doesn't help me more. We have to filter integers in an array which is saved in const variable.

const realNumberArray = [4, 5.6, -9.8, 3.14, 42, 6, 8.34, -2];
const squareList = (arr) => {
  "use strict";
const squaredIntegers = arr.filter( (num) => num > 0 && num % parseInt(num) === 0 ).map( (num) => Math.pow(num, 2) );
 return squaredIntegers;
};

const squaredIntegers = squareList(realNumberArray);
console.log(squaredIntegers);

and I cannot understand really this part num % parseInt(num) === 0 ).map( (num) => Math.pow(num, 2) );

Why using parseint() method and Math.pow. Someone could explain me why?

thanks.

Upvotes: 2

Views: 15701

Answers (7)

Beli Z
Beli Z

Reputation: 1

Here's a much easier way to fix your problem :

  const squareList = arr => {
  let squaredNumbers = arr
  .filter(elem => elem > 0 && Number.isInteger(elem))
  .map(elem => elem * elem)
  return squaredNumbers;
};

Upvotes: 0

Titurex
Titurex

Reputation: 81

A simpler way to use the reduce statement is this

const squareList = arr => {
  // Only change code below this line
  let myArr = arr.filter(user => Number.isInteger(user) && user > 0)
               .reduce((sqr,num) => sqr.concat(num*num), []);
  return myArr;
  // Only change code above this line
};

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

where Number.isInteger() is used to filter off decimals and user > 0 is used to filter off negative values from the arr

Upvotes: 0

AJAY SHARMA
AJAY SHARMA

Reputation: 11

Try This One it will not only filter out the negative once also returns only those which are Integer.

const squareList = arr => {
  let myArr = arr
        .filter(item => item > 0)
        .reduce((a, current) => a.concat(current*current) , [])
        .filter(num => Number.isInteger(num));
  return myArr;
};

const squaredIntegers = squareList([4, 5.6, -9.8, 3.14, 42, 6, 8.34, -2]);
console.log(squaredIntegers);// Prints : [16, 1764, 36] on console.

Upvotes: 1

ARDreamer
ARDreamer

Reputation: 11

const squaredIntegers = arr.filter(
    (num) => {
        if (Number.isInteger(num) && num >0){
        return num;
}}
).map((num) => num *num); // Math.pow(num, 2) can also be used here
return squaredIntegers;
};

This could be an easier solution for your problem, although all the above solutions work fine as well. num*num could also be replaced with Math.pow(num, 2).

Upvotes: 1

Khalfella
Khalfella

Reputation: 109

Many people have explained this in a very good way. You can also do this part

const squaredIntegers = arr.filter( (num) => num > 0 && num % parseInt(num) === 0 ).map( (num) => Math.pow(num, 2) );

Like this, and it should work without problems. Hopefully, this helps.

const squaredIntegers = arr.filter( (num) => num > 0 && num % 1 === 0 ).map( (num) => num * num );

Upvotes: 0

ellipsis
ellipsis

Reputation: 12152

const squaredIntegers = arr.filter( (num) => num > 0 && num % parseInt(num) === 0 ).map( (num) => Math.pow(num, 2) );
 return squaredIntegers;

Here inside the filter, it is checked if num is positive (num>0) and num is an integer. For checking for integer. num % parseInt(num) parseInt changes the num to an integer, and the modulus of a number with itself is 0 so the condition num % parseInt(num)==0. Math.pow(num,2) is used to square the num.

Upvotes: 2

Mikhail Litvinov
Mikhail Litvinov

Reputation: 452

parseInt(num) gives an integer part of num, for example, parseInt(3.14) === 3 //true. Using num % parseInt(num) basically gives a difference between the number and its integer part. If it isn't 0, the number is thrown out. Math.pow(num) gives a squared number, which is returned to the new array. Though, num * num is faster in that regard, not having to include a module and to call an object property.

Other than that, the code is very crammed in the solution, and I would suggest to break it down to improve readability. Seems like the style in which it is written adds to the confusion.

Upvotes: 4

Related Questions