Repeat String - Problem
Write code that enhances all strings such that you can call the string.replicate(x) method on any string and it will return repeated string x times.
Note: Try to implement it without using the built-in method string.repeat.
This problem requires extending the String prototype in JavaScript to add a custom method that replicates the string functionality.
Input & Output
Example 1 — Basic Repetition
$
Input:
str = "abc", times = 3
›
Output:
abcabcabc
💡 Note:
String 'abc' repeated 3 times gives 'abcabcabc'
Example 2 — Single Character
$
Input:
str = "x", times = 5
›
Output:
xxxxx
💡 Note:
Single character 'x' repeated 5 times gives 'xxxxx'
Example 3 — Zero Repetitions
$
Input:
str = "hello", times = 0
›
Output:
💡 Note:
Any string repeated 0 times gives empty string
Constraints
- 1 ≤ str.length ≤ 1000
- 0 ≤ times ≤ 100
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code