![]() | |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
if (isNan(myVariable)) { alert('incorrect, not a number'); } else { if ((myVariable % 1)!=0) { alert('Number can not be a floating-point'); } else return true; return false; } |
#3
| |||
| |||
|
|
Vincent van Beveren wrote: if (isNan(myVariable)) { alert('incorrect, not a number'); } else { if ((myVariable % 1)!=0) { alert('Number can not be a floating-point'); } else return true; return false; } Why not: function isInteger(num){ return (!isNaN(num) && num%1==0); } ? Mick |
#4
| |||
| |||
|
|
Why not: function isInteger(num){ return (!isNaN(num) && num%1==0); } ? Mick return !(isNaN(num) || !(num % 1)); or return !isNaN(num) && !!(num % 1); |
#5
| |||
| |||
|
|
return !(isNaN(num) || !(num % 1)); or return !isNaN(num) && !!(num % 1); I just wrote the code as a draft. Any of these are good. return (!isNaN(num)) && ((num % 1)==0) would probably be the most relayable... since num % 1 is a calculation, it would be better to see if it matches a number, even though 0 == false. |
#6
| |||
| |||
|
|
Vincent van Beveren wrote on 21 apr 2004 in comp.lang.javascript: return !(isNaN(num) || !(num % 1)); or return !isNaN(num) && !!(num % 1); I just wrote the code as a draft. Any of these are good. return (!isNaN(num)) && ((num % 1)==0) would probably be the most relayable... since num % 1 is a calculation, it would be better to see if it matches a number, even though 0 == false. I do not know about this relaying, but any nonzero number is considered true in javascriot. var nonzeronumber = 2; alert(!!nonzeronumber) // gives true var zeronumber = 0; alert(!!zeronumber) // gives false |
![]() |
| Thread Tools | |
| Display Modes | |
| |