String.prototype.repeatLoading saved progress…

String.prototype.repeat

String.prototype.repeat returns a new string made of count copies of the original, joined end to end with nothing between them. It's how you build a divider line ('='.repeat(40)), indentation, or any fixed-width filler.

Implement stringRepeat(str, count). Concatenate str with itself count times and return the result. A count of 0 gives ''. A negative or infinite count is invalid — throw a RangeError.

Signature

function stringRepeat(str, count) {
  // returns str concatenated `count` times; throws RangeError if count
  // is negative or Infinity.
}

Examples

stringRepeat('ab', 3); // 'ababab'
stringRepeat('=', 5);  // '====='
stringRepeat('x', 0);  // ''
stringRepeat('ab', -1);       // throws RangeError
stringRepeat('ab', Infinity); // throws RangeError

Notes

  • No separator — copies are joined directly. stringRepeat('-', 3) is '---', not '- - -'.
  • count of 0 — returns the empty string, not the original.
  • Fractional count — truncated toward zero: 2.9 means 2 repeats.
  • Negative or Infinity — throw a RangeError. These are the only inputs that throw.
  • NaN count — treated as 0, so the result is '' (no throw).
Loading editor…