# Thread: odd and even numbers

1. ## odd and even numbers

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

2. 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")
}
}
</Script>

3. 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==1 || a==3 || a==5 || a==7 || a==9) document.write('odd = ' + a + '<br>');     else document.write('even = ' + a + '<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

4. 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. 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. 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. hehe, the only problem with my code is that it only checks the first 10 numbers.

How can I read the last digit?

8. 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. basically return whether an odd or even number gets inputed into a text box.

10. a number that is inputed by the user??

11. 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">
</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. Use modulus or biwise AND?

13. How would I use bitwise, do you have an example as I never did learn that properly

14. Here is how it's done:

if( yourVariable % 2 == 0 ) alert( yourVariable + " is even." );
else alert( yourVariable + " is odd." );

15. sry, I meant bitwise operators and stuff, whats meant by (<<) & (|) & (>>>) & (^)

thanks for that btw

16. 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. thanks for that

18. 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){
}
else{
}
}
</Script>

its bullet proof

19. 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));

M@rco

20. You will probably want to use it like this:

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

M@rco

21. ## 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. Yup.

Mod: Divide, then get remainder

AND: Look at LSB

#### Posting Permissions

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