Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial

Łukasz Czuliński
8,646 PointsIndex with jQuery: possible to make numbers go: 00, 01, etc?
Hi all. I haven't had luck finding the solution to this while searching online. Does anyone know how to make the index increments go like in my post title title, as opposed to 1, 2, 3, etc?
Here is my code:
$(".team").each(function(index){
$("#teamrank p").append(index + ": " + $(this).text() + "<br />");
});
With a current output of:
1: KUL<br />
2: SIN<br />
3: BKK<br />
4: PNH<br />
5: PEN<br />
6: HKG<br />
7: HAN<br />
8: SGN<br />
9: NRT<br />
10:MNL
Any ideas on how to automatically make it go 01, 02, 03, etc?
2 Answers

Jason Anello
Courses Plus Student 94,610 PointsHi Lukasz,
There could possibly be a html/css solution here depending on how many teams there are.
If you marked up your items as an ordered list then you could use list-style-type: decimal-leading-zero;
html:
<ol>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ol>
ol {
list-style-type: decimal-leading-zero;
}
However, this property doesn't seem to be that useful in practice. I've fount that it doesn't add 2 leading zeros on the single digit numbers if you have more than 99 items, Ex. 001, ..., 099, 100. Instead it's this: 01, ..., 99, 100 It also adds a leading zero if you only have 3 items for example. 01, 02, 03
What it seems to do is add a single leading zero to single digit numbers, no matter what. You don't get leading zeros on 2 digit numbers or higher. Not sure if this is the way it is supposed to work but it doesn't seem intuitive to me.
I think that from a semantic point of view it would be better for you to mark this up as an ordered list and I would say that if you know you will have between 10 and 99 teams then this could be a viable solution.
This stackoverflow link has that solution and a javascript based solution: http://stackoverflow.com/questions/5290587/is-there-a-way-to-append-leading-zeros-to-an-ordered-number-list-01-or-001-as
If you decide you have to go with the javascript based solution then you can replace your index
variable in the output string with a call to the addzeros
function where you pass in the index
variable as the 1st argument. You also need to get the number of digits you'll have.
function addzeros(number, length) {
var num = '' + number;
while (num.length < length) {
num = '0' + num;
}
return num;
}
var numDigits = String($(".team").length).length;
$(".team").each(function(index){
$("#teamrank p").append(addzeros(index, numDigits) + ": " + $(this).text() + "<br />");
});
I didn't test this out so let me know if you run into problems with it.

Łukasz Czuliński
8,646 PointsThanks so much Jason! That was tons of help. I didn't even think about doing the list. It was exactly what I needed, as the number of teams is usually above 10 and always below 100.

Jason Anello
Courses Plus Student 94,610 PointsGlad it helped!
I should have asked a clarifying question here. Was your reason for doing this because you wanted to have right alignment of the numbers? Or did you specifically want the leading zeros for formatting/visual reasons?
I think that if you switch over to the list then you'll get right alignment of the numbers with or without the leading zeros.
I also forgot to address whether you were ok with a period after the numbers or if you really wanted a colon.

Łukasz Czuliński
8,646 PointsYep, a period was actually what I wanted. I meant to change the colon in my initial example.
And the leading zeros is purely for visual reasons, as it looks quite a bit better on the particular page.