Javascript Number pad(length, char)

Description

Javascript Number pad(length, char)


Number.prototype.pad = function(length, char) {
    if (length === undefined)
        length = 2;/*  w  ww . j a va  2 s .c o  m*/
    if (char === undefined)
        char = "0";
    return (char + this).slice(length * -1);
}

Javascript Number pad(length, char)

Number.prototype.pad = function(length, char) {
 var str = '' + this;
 while (str.length < length) {
  str = char + str;//from  ww  w. j  a  va 2  s. c  om
 }
 return str;
}

var flare = {};
flare.hosts = {};
for(var i = 1; i < 6; i++) {
 var forests = {}
 var max = Math.floor(Math.random()*11) + 1;
 for(var j = 0; j < max; j++) {
  forests["forest" + j.pad(3, "0")] = Math.floor(Math.random()*800) + 1;
 }
 flare.hosts["host" + i.pad(3, "0")] = forests;
}

console.dir(flare);



PreviousNext

Related