## 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.

`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.

**What are integers in JavaScript?,** only means that the numbers don't have a decimal fraction, and 32-bit means that they are within a certain range. Filter Definition & Syntax. The filter() method returns a new array created from all elements that pass a certain test preformed on an original array. Here’s what the syntax looks like: let newArr = oldArr.filter(callback); newArr — the new array that is returned; oldArr — the array to run the filter function on

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.

**filter() Array Method in JavaScript ← Alligator.io,** () checks it against the condition. This is useful for accessing properties, in the case of objects. Definition and Usage. The filter() method creates an array filled with all array elements that pass a test (provided as a function). Note: filter() does not execute the function for array elements without values.

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 );

**Number.isInteger(),** How do you check if value is a number JavaScript? Summary: in this tutorial, you will learn how to use the JavaScript Array filter() method to filter elements in an array.. Introduction to JavaScript array filter() method. One of the most common tasks when working with an array is to create a new array that contains a subset of elements of the original array.

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)`

.

**Checking whether a value is an integer in JavaScript,** Integers lead an odd life in JavaScript. In the ECMAScript specification, they only exist conceptually: All numbers are always floating point and array - the original array the filter was called on Having these arguments in mind, we can also write the syntax as: let newArray = array.filter(callback(element, index, array)); or let newArray = array.filter((element, index, array) => { //filter 'em elements })

**JavaScript Array filter() Method,** Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java In Javascript we have map, filter and reduce, all functions that given an initial list (array of things), transform it into something else, while keeping that same original list intact. Map

**JavaScript Number isInteger() Method,** This method returns true if the value is of the type Number, and an integer (a number without decimals). Otherwise it returns false. Browser Support. Method. filter () calls a provided callback function once for each element in an array, and constructs a new array of all the values for which callback returns a value that coerces to true. callback is invoked only for indexes of the array which have assigned values; it is not invoked for indexes which have been deleted or which have never been assigned values.

**How to filter out only Numbers in an Array using JavaScript,** An array in JavaScript can hold different types of value in a single variable. The value can be a string or a number. Sometimes you might have to separate the If you’re starting in JavaScript, maybe you haven’t heard of .map(), .reduce(), and .filter().For me, it took a while as I had to support Internet Explorer 8 until a couple years ago.