SitePoint Sponsor

User Tag List

Results 1 to 22 of 22
  1. #1
    Currently Occupied; Till Sunda Andrew-J2000's Avatar
    Join Date
    Aug 2001
    Location
    London
    Posts
    2,475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    odd and even numbers

    Is there a function to detect if a number is odd or even?

  2. #2
    SitePoint Enthusiast
    Join Date
    Mar 2002
    Location
    Birmingham, UK
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    nope but what you could do is divide the number by 2 and check to see if it has a decimal point.. if so then its an odd number

    [code]
    <Script language="JavaScript">
    function check(){
    var num = 7
    var sum = num/2
    var odd = false
    for(i=0;i>=sum.length;i++){
    if(sum.substring(i,i)=="."){
    odd = true
    }
    }
    if(odd){
    document.write(num+ "is odd")
    }
    else{
    document.write(num+ "is even")
    }
    }
    window.onload = check
    </Script>

  3. #3
    Currently Occupied; Till Sunda Andrew-J2000's Avatar
    Join Date
    Aug 2001
    Location
    London
    Posts
    2,475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hmmm I was going to try this way in the mean time.

    PHP Code:
    //for example

    for (a=0,b=50;a<b;a++)

    {
        if (
    a==|| a==|| a==|| a==|| a==9document.write('odd = ' '<br>');
        else 
    document.write('even = ' '<br>');


    but i couldn't work out how to read the last character in the integer to do this for every number.

    any way which method would be faster

    cheers 4 the reply

  4. #4
    SitePoint Enthusiast
    Join Date
    Mar 2002
    Location
    Birmingham, UK
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i dont know much about php, but does it work the same as asp (i dont wanna get into any rguments to which is better lol).. i mean does it have to reload the page to get a result from a function?... if so the javascritp will be faster because it doesnt have to reload.

    -phil

  5. #5
    Currently Occupied; Till Sunda Andrew-J2000's Avatar
    Join Date
    Aug 2001
    Location
    London
    Posts
    2,475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    lol, sorry, I like the php syntax highlighting thats all I think it makes it easier to read, that was javascript above by the way not php.


    Code:
    <Script language="JavaScript">
    //for example
    
    for (a=0,b=50;a<b;a++)
    
    {
        if (a==1 || a==3 || a==5 || a==7 || a==9) document.write('odd = ' + a + '<br>');
        else document.write('even = ' + a + '<br>');
    
    }
    </script>
    also I couldn't seem to get your code to work, well it returned even for odd numbers?.

  6. #6
    SitePoint Enthusiast
    Join Date
    Mar 2002
    Location
    Birmingham, UK
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP: <<< javascript ehy... well dont bumble the boy blunder lol
    --------------------------------------------------------------------------------


    //for example

    for (a=0,b=50;a<b;a++)

    {
    if (a==1 || a==3 || a==5 || a==7 || a==9) document.write('odd = ' + a + '<br>');
    else document.write('even = ' + a + '<br>');

    }

  7. #7
    Currently Occupied; Till Sunda Andrew-J2000's Avatar
    Join Date
    Aug 2001
    Location
    London
    Posts
    2,475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hehe, the only problem with my code is that it only checks the first 10 numbers.

    How can I read the last digit?

  8. #8
    SitePoint Enthusiast
    Join Date
    Mar 2002
    Location
    Birmingham, UK
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok ok what are you using this script for anyway? tell me what your trying to do n ill come up wiht a script for u

  9. #9
    Currently Occupied; Till Sunda Andrew-J2000's Avatar
    Join Date
    Aug 2001
    Location
    London
    Posts
    2,475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    basically return whether an odd or even number gets inputed into a text box.

  10. #10
    SitePoint Enthusiast
    Join Date
    Mar 2002
    Location
    Birmingham, UK
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    a number that is inputed by the user??

  11. #11
    Currently Occupied; Till Sunda Andrew-J2000's Avatar
    Join Date
    Aug 2001
    Location
    London
    Posts
    2,475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well it will be in a for loop

    I want to create a table that will add rows and change it's column color as the rows count up.


    The table will be like this

    Code:
    <table width="606" border="0" cellpadding="0" cellspacing="0">
      <tr> 
        <td height="21" valign="top" colspan="3">&nbsp;</td>
      </tr>
      <tr> 
        <td width="129" valign="top" rowspan="4">&nbsp;</td>
        <td valign="middle" width="313" height="19" bgcolor="#333333" align="center"> 
          <div align="center">head</div>
        </td>
        <td width="164" rowspan="4" valign="top">&nbsp;</td>
      </tr>
      <tr> 
        <td valign="middle" height="19" bgcolor="#CCCCCC" align="center"> 
          <div align="center">light odd</div>
        </td>
      </tr>
      <tr> 
        <td valign="middle" height="19" bgcolor="#999999" align="center"> 
          <div align="center">dark even</div>
        </td>
      </tr>
      <tr> 
        <td height="19" valign="middle" bgcolor="#CCCCCC" align="center"> 
          <div align="center">light odd</div>
        </td>
      </tr>
      <tr> 
        <td colspan="3" height="25" valign="top">&nbsp;</td>
      </tr>
    </table>

  12. #12
    SitePoint Evangelist
    Join Date
    Jul 2001
    Location
    UK
    Posts
    407
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use modulus or biwise AND?

  13. #13
    Currently Occupied; Till Sunda Andrew-J2000's Avatar
    Join Date
    Aug 2001
    Location
    London
    Posts
    2,475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How would I use bitwise, do you have an example as I never did learn that properly

  14. #14
    You talkin to me? Anarchos's Avatar
    Join Date
    Oct 2000
    Location
    Austin, TX
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is how it's done:

    if( yourVariable % 2 == 0 ) alert( yourVariable + " is even." );
    else alert( yourVariable + " is odd." );
    ck :: bringing chris to the masses.

  15. #15
    Currently Occupied; Till Sunda Andrew-J2000's Avatar
    Join Date
    Aug 2001
    Location
    London
    Posts
    2,475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sry, I meant bitwise operators and stuff, whats meant by (<<) & (|) & (>>>) & (^)

    thanks for that btw
    Last edited by Andrew-J2000; Mar 21, 2002 at 10:35.

  16. #16
    SitePoint Evangelist
    Join Date
    Jul 2001
    Location
    UK
    Posts
    407
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not really bother by all the others, but the AND one is useful, haven't used JavaScript for a while, but it's probaly like this:


    if( yourVariable & 0 ) alert( yourVariable + " is even." );
    else alert( yourVariable + " is odd." );

    It's to do with the way the binary system works, odd numbers have '1' as their last 'digit': 1, 10, 11, 100, 101...

  17. #17
    Currently Occupied; Till Sunda Andrew-J2000's Avatar
    Join Date
    Aug 2001
    Location
    London
    Posts
    2,475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for that

  18. #18
    SitePoint Enthusiast
    Join Date
    Mar 2002
    Location
    Birmingham, UK
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i still stand by my script, i had a think bout it last night, and any number divided by 2 has the last digit of ".5"... sooo

    [code]
    <script language="JavaScript">
    function check(){
    var number = 35
    var sum = number/2
    var length = sum.length-1
    var length2= sum.length
    if(sum.substring(length,length2)==5){
    alert("your number is odd")
    }
    else{
    alert("your number is even")
    }
    }
    </Script>

    its bullet proof

  19. #19
    The doctor is in... silver trophy MarcusJT's Avatar
    Join Date
    Jan 2002
    Location
    London
    Posts
    3,509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Apologies, but I've a neat code freak and can't resist giving the neatest solution! (I always try to write the most efficient code while maintaining versatility - I used to spend hours tweaking my old C++ code)

    <rant>
    In fact, I am quite astounded that no-one had thought to use the modulus operator until TechSited mentioned it! This is such a classic use for MODs (or bitwise operations)!!

    Everyone, PLEASE revise your basic statements, like boolean operators, mods, etc! They are incredibly handy!
    </rant>


    Try this for size:
    Code:
    function IsOdd(num)
    { 
    	return ((num % 2)? true : false);
    }
    test with:
    Code:
    alert("IsOdd(4) = " + IsOdd(4));
    alert("IsOdd(5) = " + IsOdd(5));

    M@rco
    Last edited by M@rco; Mar 24, 2002 at 12:19.

  20. #20
    The doctor is in... silver trophy MarcusJT's Avatar
    Join Date
    Jan 2002
    Location
    London
    Posts
    3,509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You will probably want to use it like this:

    Code:
    if(IsOdd(textbox.value))
    {
    	//do stuff
    }
    else
    {
    	//do other stuff
    }

    M@rco

  21. #21
    The doctor is in... silver trophy MarcusJT's Avatar
    Join Date
    Jan 2002
    Location
    London
    Posts
    3,509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    AND?!!

    In fact, using the AND bitwise operator is the most efficient approach (but it doesn't really matter, just being pedantic):

    Code:
    function IsOdd(num)
    { 
    	return (num & 1 ? true : false);
    }

    M@rco

  22. #22
    SitePoint Evangelist
    Join Date
    Jul 2001
    Location
    UK
    Posts
    407
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yup.

    Mod: Divide, then get remainder

    AND: Look at LSB


Bookmarks

Posting Permissions

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