Write a function named myRepeat that extends the String prototype. This function should allow a string to be repeated a specified number of times, enhancing the native capabilities of string manipulation in JavaScript. The native string repeat in JavaScript is String.prototype.repeat(), which you can use to check your output.
Input
- String: The string to be repeated.
- Integer (times): The number of times the string should be repeated.
Output
A new string that concatenates the original string the specified number of times.
String Repeat in JavaScript: Example
Input: "hello world ".myRepeat(2)
Output: "hello world hello world "
The function myRepeat takes an integer times as an argument and returns a new string that repeats the original string times number of times. This custom method does not modify the original string but instead returns a new string with the repeated content, demonstrating an extension of the String prototype’s functionality in JavaScript.
