There's a million ways, this is one of them, and its actually quite efficient too..
UPDATE 2
Here's code for the 'third interpretation of the OP's question
This will output ["00", "01", "02" .... ,"30"]
var max = 30, i = max, maxLength = max.toString().length, num, zeroes=[];
while (i--) zeroes.push("0");
zeroes = zeroes.join();
for (i=0; i < max ; i++) {
num = i.toString();
console.log(zeroes.substring(0, maxLength - num.length) + num);
}
UPDATE 1 adapted the code for both 'possible' interpretations of the OP's question.
If what you want is code that based on n=5 , max = 30 produces 29 "0"s followed by "5" then this is the code you want
var n = 5, max = 30;
var a = n.toString().split(), zeroesToAdd = max - a.length;
while(zeroesToAdd--) a.unshift("0");
var num = a.join("");
alert(num);
If what you want is code that based on n=5 , max = 30 produces 2 (the length of 30.toString()) "0"s followed by "5" then this is the code you want
var n = 5, maxLength = (30).toString().length;
var a = n.toString().split(), zeroesToAdd = maxLength - a.length;
while(zeroesToAdd--) a.unshift("0");
var num = a.join("");
alert(num);
The only difference here is in the first line.
These do not use string concatenation (extremely inefficient), instead they use Array.unshift
and Array.join