Q:

JavaScript repeat character

let string = 'Plumbus'
let count = 3

string.repeat(count); // -> 'PlumbusPlumbusPlumbus'
2
var a="a";
var aaa=a.repeat(3); // "aaa"
5
function repeatStringNumTimes(string, times) {
  var repeatedString = "";
  while (times > 0) {
    repeatedString += string;
    times--;
  }
  return repeatedString;
}
repeatStringNumTimes("abc", 3);
0
// hey buddy, here are different implementations, choose your favourite !!


// best implementation
repeatStr = (n, s) => s.repeat(n);


// short
function repeatStr(n, s) {
  return s.repeat(n);
}


// without using repeat
function repeatStr(n, s) {
  var str = "";
  for (var i = 0; i < n; i++) str += s;
  return str;
}
0

New to Communities?

Join the community