Javascript String repeatify(n)

Description

Javascript String repeatify(n)


//Create a repeatify function on String object -- native mthods.

String.prototype.repeatify = function(n) {
  var str = ''
  for (var i = 0; i < n; i++) {
    str += this//  ww  w .  j  a  v  a2 s . c  o  m
  }
  return str
}

console.log('hello'.repeatify(3))

Javascript String repeatify(n)

String.prototype.repeatify = function(n) {
  var results = [];
  for (var i = 0; i < n; i++) {
    results.push(this);/*from ww  w . ja v  a2 s .c om*/
  }
  return results.join(',');
};

console.log("string".repeatify(3));

Javascript String repeatify(n)

String.prototype.repeatify = function (n){

 if(n<=0) return ''
 return this + this.repeatify(n-1)
}

let aaa= 'abc'
aaa.repeatify(3) //"abcabcabc"



PreviousNext

Related