Page 1 of 1
Javascript: Sorting Odd's from Even's
Posted: Mon Feb 19, 2007 9:18 pm
by iknownothing
Hey guys,
I was wondering if there was any way to sort out odd's from even's using a javascript if statement, want it for formatting purposes for generated divs.
Posted: Mon Feb 19, 2007 9:27 pm
by Kieran Huggins
odd and even what? odd and even in what context? and what do you want to do to the elements, apply css?
Posted: Mon Feb 19, 2007 10:27 pm
by iknownothing
Kieran Huggins wrote:odd and even what? odd and even in what context? and what do you want to do to the elements, apply css?
same deal as the last question i asked and you told me about the jQuery, but now I want to put css styling on them yes, but there will be 2 rows, so thats why I need to sort out odds from evens.
eg. (doubt it would work...)
Code: Select all
while (i < w) {
if (i == oddnumber) { //odd (i = 1,3,5,7....)
even jquery thing... with css styles to make it sit further left
}
else { //even (i = 2,4,6,8....)
even jquery thing... with css styles to make it sit further right
}
}
Posted: Mon Feb 19, 2007 10:32 pm
by John Cartwright
Posted: Sat Feb 24, 2007 2:18 am
by R4000
Just to let you know, to find out if an int is odd or even you should do the following:
Code: Select all
for(i=0;i<10;i++){
if(i % 2) {
alert("The number " + i + " is an even number");
} else {
alert("The number " + i + " is an odd number");
}
}
The operator used above (%) is called the MOD operator. It allows you to check if a number is devisible by another number.
E.G:
Code: Select all
1 % 2 (is 1 devisible by 2) == false
100 % 2 (is 100 devisible by 2) == true
9 % 3 (is 9 devisible by 3) == true
You can use the MOD operator in most languages (PHP, JavaScript, C, C++, Python, ...)
Hope this helps.
Posted: Sat Feb 24, 2007 2:44 am
by nickvd
R4000 wrote:The operator used above (%) is called the MOD operator. It allows you to check if a number is divisible by another number.
... More specifically, it returns the remainder of the division.
Posted: Sat Feb 24, 2007 2:45 am
by R4000
True...
Posted: Sat Feb 24, 2007 11:08 am
by feyd
R4000 has the even/odd status flipped in the above snippet.