Quick parseint question

Quick parseint question
0

#1

Tell us what’s happening:
all i want to understand is why parseint would work in this because doesnt it turn a string to an integer so wheres the string?

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 => num * num);
  // 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/69.0.3497.100 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/es6/write-higher-order-arrow-functions/


#2

This is one of the solutions right? Someone has tried to be clever, basically. parseInt on a number will still try to parse the integer value from it. That part of the solution is saying “the nimber minus the integer part of the number == zero”, so 3.14 would be false, 3 would be true.

A much, much clearer version is n % 1 == 0 - if a number divides evenly by 1, it is by definition an integer.


#3

deja vu

It is just a bad (IMO) use of parseInt, it would be better to use Math.floor


#4

ParseInt can parse ints and floats too, always returning an int.


#5

thanks everyone ! there are so many solutions to a problem.