HighDots Forums  

Re: Integer Check

Javascript JavaScript language (comp.lang.javascript)


Discuss Re: Integer Check in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Vincent van Beveren
 
Posts: n/a

Default Re: Integer Check - 04-20-2004 , 10:47 AM






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;
}



Reply With Quote
  #2  
Old   
Mick White
 
Posts: n/a

Default Re: Integer Check - 04-20-2004 , 05:07 PM






Vincent van Beveren wrote:

Quote:
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


Reply With Quote
  #3  
Old   
Evertjan.
 
Posts: n/a

Default Re: Integer Check - 04-20-2004 , 06:30 PM



Mick White wrote on 20 apr 2004 in comp.lang.javascript:

Quote:
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

return !(isNaN(num) || !(num % 1));

or

return !isNaN(num) && !!(num % 1);


--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)


Reply With Quote
  #4  
Old   
Vincent van Beveren
 
Posts: n/a

Default Re: Integer Check - 04-21-2004 , 03:23 AM



Quote:
Why not:
function isInteger(num){
return (!isNaN(num) && num%1==0);
}
?
Mick



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.



Reply With Quote
  #5  
Old   
Evertjan.
 
Posts: n/a

Default Re: Integer Check - 04-21-2004 , 04:04 AM



Vincent van Beveren wrote on 21 apr 2004 in comp.lang.javascript:
Quote:
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

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)


Reply With Quote
  #6  
Old   
Richard Cornford
 
Posts: n/a

Default Re: Integer Check - 04-21-2004 , 05:32 AM



Evertjan. wrote:
Quote:
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
If there is a desire to explicitly return a boolean value, but avoid the
extra comparisons, it might be practical to apply the NOT operator to
the result of the whole expression:-

return !(isNaN(num)||(num % 1));

Richard.




Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.