Javascript: Sorting Odd's from Even's

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
iknownothing
Forum Contributor
Posts: 337
Joined: Sun Dec 17, 2006 11:53 pm
Location: Sunshine Coast, Australia

Javascript: Sorting Odd's from Even's

Post 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.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post 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?
User avatar
iknownothing
Forum Contributor
Posts: 337
Joined: Sun Dec 17, 2006 11:53 pm
Location: Sunshine Coast, Australia

Post 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
}
}
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

User avatar
R4000
Forum Contributor
Posts: 168
Joined: Wed Mar 08, 2006 12:50 pm
Location: Cambridge, United Kingdom

Post 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.
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post 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.
User avatar
R4000
Forum Contributor
Posts: 168
Joined: Wed Mar 08, 2006 12:50 pm
Location: Cambridge, United Kingdom

Post by R4000 »

True...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

R4000 has the even/odd status flipped in the above snippet.
Post Reply