You need Login/Signup to run/submit the code.
How to implement String.prototype.repeat? | String Polyfills | Frontend Problem Solving | JavaScript Interview Question
@Devtools Tech
In this question, the candidate needs to implement a function customRepeat
that mimics the behaviour of String.prototype.repeat
method.
More about Array.prototype.repeat
The repeat()
method constructs and returns a new string which contains the specified number of copies of the string on which it was called, concatenated together.
Syntax
customRepeat(count);
Parameters
count
An integer between 0 and +Infinity, indicating the number of times to repeat the string.
Return value
A new string containing the specified number of copies of the given string.
Exceptions
RangeError
Thrown if count is negative or if count overflows maximum string length (Infinity
).
Examples
const mood = 'Happy! ';
console.log(`I feel ${mood.customRepeat(3)}`);
// prints: "I feel Happy! Happy! Happy! "
Submission
Start the timer, complete your solution, pass all the test cases and submit it. Ideally, you should finish this question within 30 mins.