Re: JavaScript test for even or odd number
by "rudy limeback" <r937(at)interlog.com>
|
| Date: |
Mon, 13 Mar 2000 00:00:09 -0500 |
| To: |
"Comharsa" <comharsa(at)clara.net>, "HWG" <hwg-techniques(at)hwg.org> |
| |
todo: View
Thread,
Original
|
|
> I'd be interested to hear from anyone
> who can offer alternative solutions.
hi colin
well just going with the flow here, you can get your function to return
true for even and false for odd (like you wanted) like this --
function number_check(value) { return ( 1 - (value%2) ) }
notice you have to flip the result of the mod, which returns 0 for even and
1 for odd, to get 1=true (i.e. it's even) and 0=false (i.e. it's odd)
"flipping" (by subtracting something that you know is either 0 or 1 from 1)
is an old programmer's trick
note that there is a certain elegance to this solution, but it comes with a
price -- a wee amount of obfuscation -- which means you should really throw
in a comment line about what it's doing
function number_check(value) {
// returns true if value is even, false if value is odd
return ( 1 - (value%2) ) }
___________________
rudy limeback
http://www.interlog.com/~r937/
http://evolt.org
HWG hwg-techniques mailing list archives,
maintained by Webmasters @ IWA
This page is part of a preserved archive of archives.hwg.org. The site is no longer active and its content is not maintained. For enquiries about this archive, write to archive(at)iwanet.org.