Write Higher Order Arrow Functions-question regarding parseInt()

Tell us what’s happening:

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)=>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/71.0.3578.98 Safari/537.36.

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

I have a question regarding this line num % parseInt(num)===0
Does it mean parseInt(5.6) //the result is 56 and 5.6%56? and why do we need to use parseInt? Is it because that If the number is not an integer it won’t be able to be divided completely anyway?

parseInt(string, radix)

Will convert the string value to an integer (and if not a string will convert it first using toString).

In this case the following operation is used to test that the number num is not a float
since

num = 5.6 // example
parseInt(num) // 5

num % parseInt(num)===0 // false 
// 5.6 % 5 = 0.5999

If you don’t mind me asking, is this some code you’ve found somewhere or is it your solution?
Be mindful that this operation can be tricky with edge cases values, and paseInt is not a substitute for Math.floor :slight_smile:

If you want to test is a value is an integer there’s isInteger, which may be more appropriate in this case.

Hope it helps :+1:

2 Likes

This is the solution from FCC hint.
Thank you! I didn’t know that there is isinteger that we can use!
It is easier to understand!