JS Built-ins
← String101 / 195
StringString.prototype.repeatbeginner

String.prototype.repeat() β€” basic repetition

πŸ“– Usage Example
'ab'.repeat(3)    // β†’ 'ababab'
'-'.repeat(5)     // β†’ '-----'
'x'.repeat(0)    // β†’ ''

Use repeat() to create a new string with the original repeated a specified number of times.

String.prototype.repeat()

str.repeat(count) returns a new string with str repeated count times.

Challenge: Implement repeatStr(str, n) that returns str repeated n times.

repeatStr('ab', 3) // β†’ 'ababab'
repeatStr('x', 0)  // β†’ ''
πŸ’‘ Hints (2)
  • β€’ `str.repeat(0)` always returns an empty string.
  • β€’ `str.repeat(1)` returns a copy of the original string.