**Tell me why "parseInt(num) === 0"**

Tell me why "parseInt(num) === 0"

My 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 coded
const squaredIntegers = squareList(realNumberArray);
console.log(squaredIntegers);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) coc_coc_browser/78.0.136 Chrome/72.0.3626.136 Safari/537.36.

The full expression is num % parseInt(num) === 0

do you know what parseInt and the % operator do?

1 Like

I did this breakdown not much time ago, maybe it is helpful for you

7 Likes

Hello, Thanks, Have a nice day !

1 Like

Have a nice day !!!

thanks, very helpful!!!

yes you learn to be polite :wink:

@ilenia I really appreciate the explanation, I’ve been trying to understand every single line and this was super helpful. Hope you don’t mind explaining one more thing (I’m a beginner and can’t wrap my head around this specific part).

  1. const realNumberArray = [4, 5.6, -9.8, 3.14, 42, 6, 8.34, -2];
  2. const squareList = (arr) => {
    In the first line we assign the array to realNumberArray
    How is that we don’t assign arr to be equal to this array [4, 5.6, -9.8, 3.14, 42, 6, 8.34, -2]; and it is used in the code?
    I realize this might be a very basic question but I can’t find the explanation.

the global variable is passed in the array, and that makes arr have that value

Oh, thank you! that makes sense now!

you can also do it like this

<redacted>

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

If you want to compare your solution to others, use the Get a hint button on the challenge and there are alternative solutions you can compare yours to. Also, you can probably search older posts using the forum search feature or google the challenge name and find more there.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

Thank you for understanding.

I’m having the same comprehension problem. I accidentally posted a similar question before landing on this one. My problem is in understanding how % works and what exactly it’s doing.

5.2 % parseInt(5.2)===0

is parseInt splitting 5.2 into 5 and 0.2? Or is it just removing the 5? In this explanation the result is that parseInt is comparing 0.2 ===0 How is it ending up with that comparison?

parseInt makes an integer from its argument, so it keeps the part before the decimal separator

% is the reminder operator, makes a division and returns the reminder.
you have the operation 5.2 % 5, if it was a division the result would be 1 point something, with reminder operator it stops the division at a whole result, so the result of the division is
5.2 / 5 = 1 reminder 0.2
the operator then returns the reminder

I suggest you read the documentation on those things:

I didn’t like this option :

  • num > 0 && num % parseInt(num) === 0

I used the following since it makes more sense in my thinking process:

  • num > 0 && Number.isInteger(num)
2 Likes

that does much more sense - even using num % 1 === 0 does much more sense, but that is the answer provided in the guild, and people asks clarifications

1 Like