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