Why does the solution ask us to use parseInt?
This is fCC’s solution:
const squaredIntegers = arr.filter( (num) => num > 0 && num % parseInt(num) === 0 ).map( (num) => Math.pow(num, 2) )
and this was my solution:
const squaredIntegers = arr.filter((num) => num > 0 && num % 2 == 0).map((num) => Math.pow(num, 2));
I’m having a hard time understanding the difference between the two answers, and I don’t understand why it’s better to use parseInt here. I appreciate any help!
You actually just got lucky with your solution, because the tests are not robust enough. The challenge instructions want you to compute the square of only the positive integers in the array realNumberArray and store the new array in the variable squaredIntegers.
Your function actually computes the square of only even positive integers in the array. Your solution would fail if the following array was used in the tests.
const realNumberArray = [4, 5.6, -9.8, 3.14, 7, 9, 42, 6, 8.34];
Your function would return the following array for the array above:
[ 16, 1764, 36 ]
but it should return:
[ 16, 49, 81, 1764, 36 ]
Why? Because 7 and 9 are positive integers, but because your function filters only positive even numbers (i.e. 4, 42, 6), your solution does not include the 49 and 81 values in the final array.
Ahhh, understood. Okay. Yeah, I did that because the positive integers WERE even, and I didn’t think to plan for odd numbers. I guess I cheated a bit without intending to
Thanks for the explanation!