← Back to challenges

Recursion to Repeat a String n Number of Times

JavaScriptHardstringsrecursion

Instructions

Create a recursive function that takes two parameters and repeats the string n number of times. The first parameter txt is the string to be repeated and the second parameter is the number of times the string is to be repeated.

String.prototype.repeat() is not allowed

Examples

repetition("ab", 3) ➞ "ababab"

repetition("kiwi", 1) ➞ "kiwi"

repetition("cherry", 2) ➞ "cherrycherry"

Notes

The second parameter of the function is positive integer.

javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.
Next: Sort an Array by String Length