Re: JavaScript test for even or odd number

by "Comharsa" <comharsa(at)clara.net>

 Date:  Sun, 12 Mar 2000 23:37:13 -0000
 To:  <hwg-techniques(at)hwg.org>
 References:  ac
  todo: View Thread, Original
> Now I'm no programmer ....... but I'm developing a JavaScript application
> that requires a numerical value to be tested to determine if it's even or
> odd. I figured that the simplest way of doing this was to convert the
value
> to binary, eg:
>
> function number_check(value) {
>  var binary = value.toString(2);
>  if (binary.charAt(binary.length - 1) == "1") {
>   return false;
>  }
>  else {
>   return true;
>  }
> }
>
> I'd be interested to hear from anyone who can offer alternative solutions.
>
> TIA
>
> Colin

You could also use the MOD function (this isn't proper code, just to show
how it could work)

if (value % 2 == 0) number is even;
else number is odd;

HTH

Brian
comharsa(at)clara.net

HWG hwg-techniques mailing list archives, maintained by Webmasters @ IWA