Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Oct 2010
    Location
    NYC
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts

    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.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,700
    Thanks
    80
    Thanked 4,658 Times in 4,620 Posts
    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) );
        ...
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New to the CF scene
    Join Date
    Oct 2010
    Location
    NYC
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    [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
    Quote Originally Posted by Old Pedant View Post
    [code]function twoDigit(n) { return n < 10 ? "0"+n : n; }
    Maybe there was a typo somewhere? please try again.

    thanks.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,700
    Thanks
    80
    Thanked 4,658 Times in 4,620 Posts
    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    Kah Hoe Wan (12-16-2010)

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,318
    Thanks
    203
    Thanked 2,566 Times in 2,544 Posts
    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) );
    ...
    Last edited by Philip M; 12-16-2010 at 01:07 PM.

  • Users who have thanked Philip M for this post:

    Kah Hoe Wan (12-16-2010)

  • #6
    New to the CF scene
    Join Date
    Oct 2010
    Location
    NYC
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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
    •