1

I have an array in javascript. It specifies URLs for images 1-1050. My images must be named 0001.jpg, 0002.jpg ect... However my script is calling up 1.jpg, 2.jpg... I am very new to JS and am struggling to incorporate an answer from here with my script.

for (i = 0001; i < 1050; i++) {
 images.push('/images/' + i + '.jpg');
}
Community
  • 1
  • 1
user1949366
  • 465
  • 2
  • 6
  • 17
  • What problems to you have implementing that answer? http://stackoverflow.com/a/5366862/407650 – XCS Aug 13 '13 at 11:54

11 Answers11

9

To combine what you linked and yours:

for (i = 1; i < 1050; i++) {
  var str = "" + i
  var pad = "0000"
  str = pad.substring(0, pad.length - str.length) + str
  images.push('/images/' + str + '.jpg');
}

I hope that helps

Mattsjo
  • 627
  • 5
  • 11
  • Sorry, I accidentally posted it at first and kept on having to edit it. The version as it is now should work. If it doesn't please tell me – Mattsjo Aug 13 '13 at 11:55
  • Easiest to read for me. Although, had to add pad to images.push to work fully. – user1949366 Aug 13 '13 at 12:08
  • oops. Still wasn't quite perfect. I'm glad it worked, though. Please accept it if you like it! – Mattsjo Aug 13 '13 at 12:14
2

You can make a utility function which convert a integer to a 4 digits string representation:

function intTo4digitsString(nb) {
   if(nb > 0 && nb < 10)
        return "000"+nb;
   else if(nb >= 10 && nb < 100)
        return "00"+nb;
   else if(nb >= 100 && nb < 1000)
        return "0"+nb;
}

This version is limited to 4 digits (and not as easily extensible than other answers), but I think it's more readable fr you :) Then you can use:

for (i = 1; i < 1050; i++) {
    images.push('/images/' + inTo4DigitsString(i) + '.jpg');
}
Maxime Lorant
  • 34,607
  • 19
  • 87
  • 97
1

I would do something like this

for (i = 1; i < 1050; i++)
{
    var name = '0000' + i;
    images.push('/images/' + name.substr(name.length - 4) + '.jpg');
}

fiddle: http://jsfiddle.net/4z5Sd/

Matías Cánepa
  • 5,770
  • 4
  • 57
  • 97
1

Thanks mattsjo. Had to add 'pad' to array for it to work fully.

for (i = 1; i < 1050; i++) {
  var str = "" + i
  var pad = "0000"
  var padd = pad.substring(0, pad.length - str.length) + str
  images.push('/images/' + padd + '.jpg');
}
user1949366
  • 465
  • 2
  • 6
  • 17
1

If you want a general purpose string formatting function then the sprintf library might work for you.

var i, images = [];

for (i = 1; i <= 1050; i++) {
    images.push(sprintf('/images/%04d.jpg', i));
}

JSFiddle

Dustin Kingen
  • 20,677
  • 7
  • 52
  • 92
  • Having used this before, i'd suggest you profile it in jsperf. It's not a great function to be using in a loop. – Craig Aug 13 '13 at 12:40
0
zeropad = function( n,l ){ 
    var ret, d, i;
    ret = n === null || n === undefined ? '' : n.toString();
    // do we need to pad or truncate?
    d = l - ret.length;
    if (d>0) {
        for (i=0;i<d; i++) {
            ret = '0'+ret;
        }
    }
    return(ret);
};
images.push('/images/'+zeropad(i,4) + '.jpg');

From a library I wrote a while back. https://github.com/deitch/jsorm-utilities

deitch
  • 14,019
  • 14
  • 68
  • 96
0
function padNum(num,length)
{
    return Array((length+1)-num.toString().length).join("0")+num;
}

padNum(1,4);//0001
padNum(250,4);//0250

And then in your code:

images.push('/images/' + padNum(i,4) + '.jpg');
MDEV
  • 10,730
  • 2
  • 33
  • 49
0

How about...

for (i = 1; i < 1050; i++) {        
    images.push('/images/' + ('0000'.substring(0, i.toString().length)+i) + '.jpg');
}
WTK
  • 16,583
  • 6
  • 35
  • 45
0

What about this:

for (var i = 1;i<1050;i++){
    images.push("/images/"+(Array(5-(""+i).length)).join("0")+i+".jpg");
} 
Chris Charles
  • 4,406
  • 17
  • 31
0

I'd suggest:

Number.prototype.leftPad = function (len) {
    var l = this.toString().length,
        d = len - l;
    return new Array(d + 1).join('0') + this.toString();
};

for (var i = 1, len = 1051; i < len; i++) {
    console.log(i.leftPad(4) + '.jpg');
}

JS Fiddle demo.

References:

David Thomas
  • 249,100
  • 51
  • 377
  • 410
0
function pad(val, len) {
    return ("000000000000000000" + val).substr(len * -1);
}

Usage:

console.log(pad(1, 4) + ".jpg");
> 0001.jpg
SztupY
  • 10,291
  • 8
  • 64
  • 87
Craig
  • 4,268
  • 4
  • 36
  • 53