Write Higher Order Arrow Functions Remainder Use Math

Tell us what’s happening:
I’ve solved the problem but I don’t understand part of the suggested answer. I used Number.isInteger() to check for integer numbers, but the solution in the hint seems to use (num % parseInt(num) === 0) for that part. I don’t understand what that actually does mathematically or how it confirms the number is an integer. I googled best I could and got no where. Anyone able to explain?

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 && Number.isInteger(num)).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/72.0.3626.119 Safari/537.36.

It’s a common way in Javascript to ensure you are only dealing with positive integers (no decimals) as stated in the final paragraph of the lesson:

the square of only the positive integers (decimal numbers are not integers)

I found the following article that might help to break it down in a more understandable way: http://2ality.com/2014/05/is-integer.html

One thing to keep in mind is that often times there is more than one way to solve a problem, so just because your method doesn’t follow the hint way, does not mean your method was wrong.

Yeah, that’s what I found, too. I get that num % 1 === 0 tests for integer, and I get that parseInt(num) kinda truncates to an integer, I just don’t get why we would use parseInt(num) as the divisor for the check. It seems to me like combining two different approaches and I don’t know why we would want to do that. Especially because of the limitations of parseInt() and % mentioned in the article you suggested.

I’m just trying to figure out the thought process, even though mine passes just fine. Thanks for the response, though!

A few of other things to keep in mind.

  • Number.isInteger() was only “recently introduced” to the JavaScript language. Often times, the basic examples shy away from using these more modern methods. (Modern being since the 2015 Release of JavaScript).

  • Number.isInteger() would cause a JavaScript error on any webpage on Internet Explorer at this point. So if you were trying to support IE for some odd reason (more people use it globally than safari as an example), you would have to make sure you avoid it or use a polyfill. See: https://caniuse.com/#search=isInteger

  • As for what the person who wrote the solution that uses (num % parseInt(num) === 0) was thinking, the solutions and hints are often community supplied. They don’t imply being the ideal solution, nor the most thought out. Perhaps they also just submitted one that works without too much thought going into it.

Near the end of the guide they have a section title “NOTES FOR CONTRIBUTIONS.” If you wanted to improve the experience of future students you could look into contributing possible solutions to the hints. https://guide.freecodecamp.org/certifications/javascript-algorithms-and-data-structures/es6/write-higher-order-arrow-functions/

% gives you the reminder of the division between the two numbers. So if for example num is 5.3 this is what happens:

5.3 % parseInt(5.3) === 0
5.3 % 5 === 0 
0.3 === 0
false

If instead you have an integer

6 % parseInt(6) === 0
6 % 6 === 0 // exact division, the reminder is 0
0 === 0
true
1 Like

This has been brought up a few times: it’s a slightly bizarre way to do it. The example probs needs updating as it’s confused quite a few people. The reason it’s done is what @ilenia says, but n % 1 == 0 would have been totally fine in this case and much less cryptic

1 Like

Another way to express whether a number is a whole number, which IMHO is more straightforward:
n === Math.floor(n)

But pretty much everything should have Number.isInteger nowadays

1 Like

This is basically what I was looking for. I didn’t understand the decimal as the remainder. It is basically the same as num % 1 would be, just seems unnecessarily complicated. Thanks!

An alternative code solution was added that is using isInteger (site not updated).

Not really sure why the old was kept the way it is, using num % 1 is so much simpler and would likely avoid some confusion. I feel like the guide should at least explain the use of parseInt if nothing else. I opened an issue to ask if a change is needed.

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

This means that the “filtered num” (1st part), is then passed on to .map() (2nd part), to be the squared?
My question is regarding the order of procedures, as I’m a bit confused about what…

Yes, the order of the method chaining matters. You are correct in saying that .filter() is returning a new array to .map(). You can always break it up using intermediate variables if it makes it more clear.

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

Or you can return the final array instead of using any intermediate variables

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