+ 1

Hi why is the isInteger not working

I want to write a function to roll a dice. It has a parameter of the number of faces but when I check to make sure it's an integer, but doesn't seem to work. Thanks Graeme https://code.sololearn.com/WYIcdn2lquMr/?ref=app

1st Nov 2022, 8:41 AM
Graeme Adamson
Graeme Adamson - avatar
3 Answers
+ 7
write only if (Number.isInteger(inValue)) if it is true and you compare true == false it would evaluate to false
1st Nov 2022, 9:27 AM
Lisa
Lisa - avatar
+ 3
Try to convert <inValue> argument into a number before verifying it as integer using Number.isInteger(). inValue = Number( inValue ); if( !Number.isInteger( inValue ) ) // <- notice the logical NOT operator `!` { // it's not an integer } The type of <inValue> is string, therefore it doesn't count as integer, as it was not even a numeric data.
1st Nov 2022, 10:08 AM
Ipang
0
Graeme Adamson tweaked your code. The main problem is 'input.value' is still type string. A quick trick is to add a + symbol in front to convert it to int. https://code.sololearn.com/WJKluCxsCRmK/?ref=app
3rd Nov 2022, 5:07 AM
Bob_Li
Bob_Li - avatar