Enjoy an ad free experience by logging in. Not a member yet? Register.

Results 1 to 6 of 6
Thread: Javascript even or odd

01092010, 07:21 AM #1
 Join Date
 Jan 2010
 Posts
 1
 Thanks
 0
 Thanked 0 Times in 0 Posts
Javascript even or odd
Hi everyone, can someone please help me to find a way to rewrite this code
function evenorodd( form )
{
var x = new Number( form.x.value );
if( isNaN( x )  x == "" )
{
alert( "Not a number or no value entered" );
return "";
}
for( var i = 0; i <= x; i++ )
( i % 2 == 0 ) ? alert( i + " is even!" ) : alert( i + " is odd!" );
}
the purpose is to show an alert box with a odd or even number
Thanks in advanced!
01092010, 07:35 AM
#2
 Join Date
 Mar 2007
 Location
 N/A
 Posts
 14,861
 Thanks
 160
 Thanked 2,223 Times in 2,210 Posts
 Blog Entries
 1
Use getElementById() and parseInt() to get the integer value from the form element.
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. (Dr. APJ. Abdul Kalam)
01092010, 07:41 AM
#3
 Join Date
 Jun 2002
 Location
 London, England
 Posts
 18,079
 Thanks
 203
 Thanked 2,542 Times in 2,520 Posts
Sounds like homework!
What is the point of the loop? You do not want an alert for each value from 0  xxx.Code:<form name = "myform"> OE<input type = "text" name = "theNumber" onblur = "evenorodd(this.value)"> </form> <script type = "text/javascript"> function evenorodd(which) { var x = parseInt(which); if( isNaN(x)  x == "" ) { alert( "Not a number or no value entered" ); return false; } ( x % 2 == 0 ) ? alert( x + " is even!" ) : alert( x + " is odd!" ); } </script>
BTW, the time to say "thanks" is afterwards, not beforehand which gives the  doubtless unintended  impression that you take other people's voluntary unpaid assistance for granted. Or as British politician Neil Kinnock put it, "Don't belch before you have had the meal." Prefer to use "please" beforehand and if you find a response helpful then you can use the "Thank User For This Post" button.
"Of course mistakes are made. Some of them are even accidental".  Channel 5
01092010, 10:07 PM
#4
So MANY ways to do this!
Besides what has already been shown:
Though I admit that the middle 2 there probably only work if Math.abs(x) < Math.pow(2,31).Code:function evenorodd(which) { var x = parseInt(which); if( isNaN(x)  x == "" ) { alert( "Not a number or no value entered" ); return false; } alert( x + [" is even"," is odd"][x % 2] ); alert( x + [" is even"," is odd"][x & 1] ); alert( x + ( ((x >> 1) << 1) == x ? " is even" : " is odd" ) ); alert( x + ( Math.floor(x / 2) * 2 == x ? " is even" : " is odd" ) ); }
And there are other ways.
Last edited by Old Pedant; 01092010 at 10:09 PM. Reason: typo
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
07152011, 11:19 PM
#5
 Join Date
 Jul 2011
 Posts
 1
 Thanks
 0
 Thanked 0 Times in 0 Posts
Simple Javascript Program
I came across http://rindovincent.blogspot.com/p/javascript.html where there was a simple Javascript program to find whether the number is odd or even. I am pasting the same code with permission here.
Code:<html> <head> <script type="text/javascript"> var n = prompt("Enter a number to find odd or even", "Type your number here"); n = parseInt(n); if (isNaN(n)) { alert("Please Enter a Number"); } else if (n == 0) { alert("The number is zero"); } else if (n%2) { alert("The number is odd"); } else { alert("The number is even"); } </script> </head> <body> </body> </html>
07152011, 11:48 PM
#6
Not to ask silly questions, but...
(1) How is that one whit different than Philip's answer?
(2) What's the point of showing the same code, 18 months later?