--- id: afcc8d540bea9ea2669306b6 title: Repeat a String Repeat a String isRequired: true challengeType: 5 --- ## Description
Repeat a given string str (first argument) for num times (second argument). Return an empty string if num is not a positive number. Remember to use Read-Search-Ask if you get stuck. Write your own code.
## Instructions
## Tests
```yml tests: - text: repeatStringNumTimes("*", 3) should return "***". testString: assert(repeatStringNumTimes("*", 3) === "***", 'repeatStringNumTimes("*", 3) should return "***".'); - text: repeatStringNumTimes("abc", 3) should return "abcabcabc". testString: assert(repeatStringNumTimes("abc", 3) === "abcabcabc", 'repeatStringNumTimes("abc", 3) should return "abcabcabc".'); - text: repeatStringNumTimes("abc", 4) should return "abcabcabcabc". testString: assert(repeatStringNumTimes("abc", 4) === "abcabcabcabc", 'repeatStringNumTimes("abc", 4) should return "abcabcabcabc".'); - text: repeatStringNumTimes("abc", 1) should return "abc". testString: assert(repeatStringNumTimes("abc", 1) === "abc", 'repeatStringNumTimes("abc", 1) should return "abc".'); - text: repeatStringNumTimes("*", 8) should return "********". testString: assert(repeatStringNumTimes("*", 8) === "********", 'repeatStringNumTimes("*", 8) should return "********".'); - text: repeatStringNumTimes("abc", -2) should return "". testString: assert(repeatStringNumTimes("abc", -2) === "", 'repeatStringNumTimes("abc", -2) should return "".'); - text: The built-in repeat()-method should not be used testString: assert(!/\.repeat/g.test(code), 'The built-in repeat()-method should not be used'); ```
## Challenge Seed
```js function repeatStringNumTimes(str, num) { // repeat after me return str; } repeatStringNumTimes("abc", 3); ```
## Solution
```js function repeatStringNumTimes(str, num) { if (num < 0) return ''; return num === 1 ? str : str + repeatStringNumTimes(str, num-1); } repeatStringNumTimes("abc", 3); ```