Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    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!

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,851
    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)

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,907
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Sounds like homework!

    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>
    What is the point of the loop? You do not want an alert for each value from 0 - xxx.

    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

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,121
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    So MANY ways to do this!

    Besides what has already been shown:

    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" ) );
    
    }
    Though I admit that the middle 2 there probably only work if Math.abs(x) < Math.pow(2,31).

    And there are other ways.
    Last edited by Old Pedant; 01-09-2010 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.

  • #5
    New to the CF scene
    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>

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,121
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    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?


  •  

    LinkBacks (?)

    1. 05-30-2014, 05:52 AM

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •