Write Higher Order Arrow Functions: 'hint' answer is confusing

The ‘hint’ anwer in this section is confusing me.

    const squareList = (arr) => {
      "use strict";
      const squaredIntegers = arr.filter( (num) => num > 0 && num % parseInt(num) === 0 ).map( (num) => Math.pow(num, 2) );
      return squaredIntegers;
    };

    // test your code
    const squaredIntegers = squareList(realNumberArray);
    console.log(squaredIntegers);

I understand most of it, but I don’d get why they use:

num % parseInt(num) === 0

wouldn’t using this:

num % 1 === 0

be more efficient?

also, why does

num % num === 0

remove the decimals and negatives? Doesn’t dividing any number by itself equal 1 so the remainder would equal 0?

1 Like

The negatives are removed by num > 0

Now,
num % parseInt(num) === 0

Let’s use the number 5.2
5.2 % parseInt(5.2) === 0
parseInt makes an integer of whatever is inside it
5.2 % 5 === 0
0.2 === 0
false

Or with a number like 6
6 % parseInt(6) === 0
6 % 6 === 0
0 === 0
true

Yes, it would work the same and be easier to understand with num % 1 === 0 but the result is the same

2 Likes

You know what, I feel dumb about the negative part, after you pointed it out, I remember seeing that. for some reason I thought about that at the very end and added it.

It’s been a while since I’ve taken a math class and seem to have forgotten that an integer is a whole number. Thank you so much, I was afraid to move forward since I didn’t understand this part.

BTW, a much simpler way to determine if a number is an integer is Number.isInteger(num). That method was also added in ES6, so it’s likely the solution author just didn’t know about it.