Javascript String repeat(num)

Description

Javascript String repeat(num)

String.prototype.repeat = function(num)
  {return new Array(num+1).join(this)}

Javascript String repeat(num)

// String.prototype.repeat
String.prototype.repeat = String.prototype.repeat || function(num) {
  return Array(num + 1).join(this);
};

Javascript String repeat(num)

String.prototype.repeat = String.prototype.repeat || function(num){
  var str = '';
  for(var i = 0; i < num; i++){
    str += this;//  w  ww.j ava  2s . c  o  m
  }
  return str;
};

Javascript String repeat(num)

String.prototype.repeat = function(num) {
  var self = this;
  for (var i = 0; i < num; i++) {
    self += this;/*from  ww  w . j a  va 2s  . c  o  m*/
  }
  return self;
}

var str = 'hello';
console.log(str.repeat(1)); // hellohello

Javascript String repeat(num)

String.prototype.repeat = function (num) {
  var str = '';
  if (num < 0) {
    return str;//from   ww w .  j  a  v  a  2  s. c  o m
  } else {
    for (var i = 0; i < num; i++) {
      str += this;
    }
    return str;
  }
}

var a = 'abc';
console.log(a.repeat(3));
// abcabcabc

Javascript String repeat(num)

String.prototype.repeat = function (num) {
  var a = [];// w  ww  . j  av  a  2 s.c o m
  a.length = num << 0 + 1;
  return a.join(this);
};

Javascript String repeat(num)

// Some general UI pack related JS
// Extend JS String with repeat method
String.prototype.repeat = function (num) {
    return new Array(num + 1).join(this);
};

Javascript String repeat(num)

String.prototype.repeat = function(num){
    return new Array( num + 1 ).join( this );
}

Array.prototype.indexOfgenericId = function(id, pos) {
    for (var i = 0; i < this.length; i++)
        if (this[i].genericId === id){
            return this[i]._id; i=0; break;}
    return (this[pos] || '' && pos == -1)? this[pos]._id : null;      
}

// polje, reverse!, (parseInt, parseFloat)
function sortBy(field, reverse, primer){

   var key = function (x) {return primer ? primer(x[field]) : x[field]};

   return function (a,b) {
       var A = key(a), B = key(b);

       return ((A < B) ? -1 : (A > B) ? + 1 : 0) * [-1,1][+!!reverse];                  
   }/*from   ww w  .  j ava2s  .  c  om*/
}

exports.sortBy = sortBy;

Javascript String repeat(num)

String.prototype.repeat = function(num){
    return new Array( num + 1 ).join( this );
}

String.prototype.rjust = function(width, padding) {
    padding = padding || " ";
    padding = padding.substr(0, 1);/*w  w  w  . j av a2  s  .  c om*/
    
    if (this.length < width) {
        return padding.repeat(width - this.length) + this;
    } else {
        return this;
    }
}

String.prototype.spacify = function() {
    var sentence = this.replace(/__/g, '-').replace(/_/g, ' ');
    if (sentence.match(/\s/) && !sentence.match(/\-/)) {
      var words = sentence.split(' ');
      for (i in words) {
        words[i] = words[i][0].toUpperCase() + words[i].substr(1);
      }
      sentence = words.join(' ');
    }
    return sentence.replace(/\-/g, '_');
}



PreviousNext

Related