I don't understand what is happening logically here. Can someone explain num%parseInt(num)==0

Tell us what’s happening:

Your code so far


const realNumberArray = [4, 5.6, -9.8, 3.14, 42, 6, 8.34, -2];
const squareList = (arr) => {
  "use strict";
  // change code below this line
  const squaredIntegers = arr.filter( (num) => num > 0 && **num % parseInt(num) == 0**).map( (num) => Math.pow(num, 2) );
  // change code above this line
  return squaredIntegers;
};
// test your code
const squaredIntegers = squareList(realNumberArray);
console.log(squaredIntegers);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36.

Which part of the expression do you not understand?

I assume you know what num represents. If you do not understand parseInt, then I suggest you review this challenge. If you are not sure what the % means, then I suggest reviewing this challenge.

I understand what parseInt() and the % operator do. I don’t understand the logic behind it. Why does

num % parseInt(num) == 0

filter out the decimal numbers?

Let’s look at an example:

Say num = 10, then (10 % parseInt(10)) will be 0 and when you compare that to 0, you know that it is an integer and not a decimal number.

On the other hand, if num = 5.2, then 5.2 % parseInt(5.2) will not be zero? Why? Because 5.2 % 5 approximately 0.2.

Basically, if num has a fraction (meaning it is a decimal numbers) of any kind, then it will always result in the expression evaluating to something other than 0.

1 Like

Oh. I realized that array.filter() makes a new array of the items that pass the given conditions, it doesn’t remove the ones that fail. That makes sense now

1 Like