2018-10-04 14:37:37 +01:00
---
id: afcc8d540bea9ea2669306b6
title: Repeat a String Repeat a String
challengeType: 5
2019-07-31 11:32:23 -07:00
forumTopicId: 16041
2018-10-04 14:37:37 +01:00
---
## Description
< section id = 'description' >
Repeat a given string < code > str< / code > (first argument) for < code > num< / code > times (second argument). Return an empty string if < code > num< / code > is not a positive number.
< / section >
## Instructions
< section id = 'instructions' >
< / section >
## Tests
< section id = 'tests' >
```yml
tests:
2018-10-20 21:02:47 +03:00
- text: < code > repeatStringNumTimes("*", 3)</ code > should return < code > "***"</ code > .
2019-05-20 18:08:20 -07:00
testString: assert(repeatStringNumTimes("*", 3) === "***");
2018-10-20 21:02:47 +03:00
- text: < code > repeatStringNumTimes("abc", 3)</ code > should return < code > "abcabcabc"</ code > .
2019-05-20 18:08:20 -07:00
testString: assert(repeatStringNumTimes("abc", 3) === "abcabcabc");
2018-10-20 21:02:47 +03:00
- text: < code > repeatStringNumTimes("abc", 4)</ code > should return < code > "abcabcabcabc"</ code > .
2019-05-20 18:08:20 -07:00
testString: assert(repeatStringNumTimes("abc", 4) === "abcabcabcabc");
2018-10-20 21:02:47 +03:00
- text: < code > repeatStringNumTimes("abc", 1)</ code > should return < code > "abc"</ code > .
2019-05-20 18:08:20 -07:00
testString: assert(repeatStringNumTimes("abc", 1) === "abc");
2018-10-20 21:02:47 +03:00
- text: < code > repeatStringNumTimes("*", 8)</ code > should return < code > "********"</ code > .
2019-05-20 18:08:20 -07:00
testString: assert(repeatStringNumTimes("*", 8) === "********");
2018-10-20 21:02:47 +03:00
- text: < code > repeatStringNumTimes("abc", -2)</ code > should return < code > ""</ code > .
2019-05-20 18:08:20 -07:00
testString: assert(repeatStringNumTimes("abc", -2) === "");
- text: The built-in < code > repeat()</ code > method should not be used.
testString: assert(!/\.repeat/g.test(code));
- text: < code > repeatStringNumTimes("abc", 0)</ code > should return < code > ""</ code > .
testString: assert(repeatStringNumTimes("abc", 0) === "");
2018-10-04 14:37:37 +01:00
```
< / section >
## Challenge Seed
< section id = 'challengeSeed' >
< div id = 'js-seed' >
```js
function repeatStringNumTimes(str, num) {
return str;
}
repeatStringNumTimes("abc", 3);
```
< / div >
< / section >
## Solution
< section id = 'solution' >
```js
function repeatStringNumTimes(str, num) {
2019-05-20 18:08:20 -07:00
if (num < 1 ) return ' ' ;
2018-10-04 14:37:37 +01:00
return num === 1 ? str : str + repeatStringNumTimes(str, num-1);
}
repeatStringNumTimes("abc", 3);
```
< / section >