Javascript String repeat(n)

Description

Javascript String repeat(n)


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

console.log("ha".repeat(5));  // hahahahaha

Javascript String repeat(n)

String.prototype.repeat = function (n) {
  var s = '';
  for (var i = 0; i < n; i++) s+= this;
  return s;//from w  w  w.  j a v  a  2 s . co  m
}

Javascript String repeat(n)

function christmasTree(height) {
    var tree = [];
    for (var i = 1; i <= height; i++) {
        tree.push(" ".repeat(height - i) + "*".repeat((i - 1) * 2 + 1) + " ".repeat(height - i));
    }/*from  w w w. j  av a2  s  . c  om*/
    return tree.join("\n");
}

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

Javascript String repeat(n)

/*!/*w  ww.ja  va 2 s . co m*/
 @name: repeat
 @desc: Repeat a character n times

 @param: {Number} n | How often to repeat the character

 @return: {String} | Some characters
**/
String.prototype.repeat = function(n) {
    n = n || 1;
    return Array(n + 1).join(this);
};

Javascript String repeat(n)

String.prototype.repeat = (function (n) {
  var s = "";
  for (var i = 0; i < n; i++) {
    s += this;//from   www.j a v a2s. co  m
  }
  return s;
});

Javascript String repeat(n)

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

var size = 10;/*from  w w w. ja  va  2 s  .c  o  m*/

for (var b = 1; b < size; b++) {
  if (b % 2) {
    console.log('# '.repeat(size/2));
  } else {
    console.log(' #'.repeat(size/2));
  }
}



PreviousNext

Related