1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Javascript even or odd

Discussion in 'HTML & JavaScript' started by h1tman, Jan 9, 2010.

  1. h1tman

    h1tman Junior Member

    Joined:
    Oct 2, 2009
    Messages:
    148
    Likes Received:
    16
    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. maddog39

    maddog39 Newbie

    Joined:
    Sep 7, 2009
    Messages:
    11
    Likes Received:
    1
    Occupation:
    Student
    Location:
    Pennsylvania, US
    Home Page:
    Code:
    if (number % 2 == 0) 
        alert("is even");
    else
        alert("is odd");