Sunny-117 / js-challenges

✨✨✨ Challenge your JavaScript programming limits step by step

Home Page:https://juejin.cn/column/7244788137410560055

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

String.prototype.repeat

Sunny-117 opened this issue · comments

 String.prototype.repeat= function (n) {
    let str = this;
    let res = ''
    while (n) {
        res += str;
        n--
    }
    return res
}
console.log('abc'.repeat(3));
String.prototype.repeat = function(count) {
  if (count < 0) {
    throw new RangeError("Count must be non-negative");
  }

  let repeatedString = "";
  for (let i = 0; i < count; i++) {
    repeatedString += this;
  }
  return repeatedString;
};