Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: help making numbers counternshow double digits like.. 00/00

1. ## help making numbers counternshow double digits like.. 00/00

I am using a great js gallery script from ....http://coffeescripter.com/code/ad-gallery/

they have a counter for the number of images shown, the number changes as user clicks on next bottom.

from looking at the script, the counter is anchor to .ad-info and the counter code from the JS is .....

_afterShow: function() {
this.gallery_info.html((this.current_index + 1) + ' / '+ this.images.length);
if(!this.settings.cycle) {

Can anyone help make the number show double digits only for the numbers 1,2,3,4,5,6,7,8,9 (example- 03/07 or 09/28 or 03/58).. etc..etc

thanks for looking.

• Code:
```function twoDigit(n) { return n < 10 ? "0"+n : n; }

_afterShow: function() {
this.gallery_info.html(
twoDigit(this.current_index + 1) + ' / '+ twoDigit(this.images.length) );
...```

• Originally Posted by Old Pedant
[code]function twoDigit(n) { return n < 10 ? "0"+n : n; }

_afterShow: function() {
this.gallery_info.html(
twoDigit(this.current_index + 1) + ' / '+ twoDigit(this.images.length) );
...
hey old pedant

its giving me a syntax error on line
Originally Posted by Old Pedant
[code]function twoDigit(n) { return n < 10 ? "0"+n : n; }
Maybe there was a typo somewhere? please try again.

thanks.

• Nope. Problem is on your end.

Works like a charm:
Code:
```<script>
function twoDigit(n) { return n < 10 ? "0"+n : n; }

document.write( "<hr>Testing twoDigit(3)/twoDigit(9): " + twoDigit(3) + "/" + twoDigit(9) );

document.write( "<hr>Testing twoDigit(1)/twoDigit(22): " + twoDigit(1) + "/" + twoDigit(22) );
</script>```
Maybe you put it in the wrong place in your code.

Safest thing is to put it directly after the <script> tag.

• ## Users who have thanked Old Pedant for this post:

Kah Hoe Wan (12-16-2010)

• Be aware that the returned value is a string, not a number. So

this.gallery_info.html((this.current_index + 1)
will cause the script to fail if this.current_index = 01 etc. "01" + 1 = 011

twoDigit("011")/twoDigit(9) // error!!

You should use the values with 0 prefix for display only, not for any kind of calculation.

This ought to work:-

Code:
```function twoDigit(n) { return n < 10 ? "0"+n : n; }

_afterShow: function() {
var str2num = Number(this.current_index);  // example 07
var num2str = twoDigit(str2num + 1); // example 08
this.gallery_info.html(num2str + ' / ' + twoDigit(this.images.length) );
...```

• ## Users who have thanked Philip M for this post:

Kah Hoe Wan (12-16-2010)

• thanks Old pedant and Phillip, both version of the code works. my site would look just ok without.

•

#### Posting Permissions

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