...

View Full Version : help making numbers counternshow double digits like.. 00/00



Kah Hoe Wan
12-16-2010, 02:01 AM
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.

Old Pedant
12-16-2010, 03:08 AM
function twoDigit(n) { return n < 10 ? "0"+n : n; }

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

Kah Hoe Wan
12-16-2010, 03:36 AM
[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
[code]function twoDigit(n) { return n < 10 ? "0"+n : n; }
Maybe there was a typo somewhere? please try again.

thanks.

Old Pedant
12-16-2010, 05:09 AM
Nope. Problem is on your end.

Works like a charm:


<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.

Philip M
12-16-2010, 09:21 AM
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:-


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) );
...

Kah Hoe Wan
12-16-2010, 09:29 PM
thanks Old pedant and Phillip, both version of the code works. my site would look just ok without.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum