Javascript: Sorting Odd's from Even's
Moderator: General Moderators
- iknownothing
- Forum Contributor
- Posts: 337
- Joined: Sun Dec 17, 2006 11:53 pm
- Location: Sunshine Coast, Australia
Javascript: Sorting Odd's from Even's
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.
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.
- Kieran Huggins
- DevNet Master
- Posts: 3635
- Joined: Wed Dec 06, 2006 4:14 pm
- Location: Toronto, Canada
- Contact:
- iknownothing
- Forum Contributor
- Posts: 337
- Joined: Sun Dec 17, 2006 11:53 pm
- Location: Sunshine Coast, Australia
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.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?
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
}
}
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
- R4000
- Forum Contributor
- Posts: 168
- Joined: Wed Mar 08, 2006 12:50 pm
- Location: Cambridge, United Kingdom
Just to let you know, to find out if an int is odd or even you should do the following:
The operator used above (%) is called the MOD operator. It allows you to check if a number is devisible by another number.
E.G:
You can use the MOD operator in most languages (PHP, JavaScript, C, C++, Python, ...)
Hope this helps.
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");
}
}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) == trueHope this helps.