2018-10-04 13:37:37 +00:00
---
id: afcc8d540bea9ea2669306b6
title: Repeat a String Repeat a String
challengeType: 5
2019-07-31 18:32:23 +00:00
forumTopicId: 16041
2021-01-13 02:31:00 +00:00
dashedName: repeat-a-string-repeat-a-string
2018-10-04 13:37:37 +00:00
---
2020-11-27 18:02:05 +00:00
# --description--
Repeat a given string `str` (first argument) for `num` times (second argument). Return an empty string if `num` is not a positive number. For the purpose of this challenge, do *not* use the built-in `.repeat()` method.
# --hints--
`repeatStringNumTimes("*", 3)` should return `"***"` .
```js
assert(repeatStringNumTimes('*', 3) === '***');
2018-10-04 13:37:37 +00:00
```
2020-11-27 18:02:05 +00:00
`repeatStringNumTimes("abc", 3)` should return `"abcabcabc"` .
2018-10-04 13:37:37 +00:00
2020-11-27 18:02:05 +00:00
```js
assert(repeatStringNumTimes('abc', 3) === 'abcabcabc');
```
2018-10-04 13:37:37 +00:00
2020-11-27 18:02:05 +00:00
`repeatStringNumTimes("abc", 4)` should return `"abcabcabcabc"` .
2018-10-04 13:37:37 +00:00
```js
2020-11-27 18:02:05 +00:00
assert(repeatStringNumTimes('abc', 4) === 'abcabcabcabc');
```
2018-10-04 13:37:37 +00:00
2020-11-27 18:02:05 +00:00
`repeatStringNumTimes("abc", 1)` should return `"abc"` .
```js
assert(repeatStringNumTimes('abc', 1) === 'abc');
2018-10-04 13:37:37 +00:00
```
2020-11-27 18:02:05 +00:00
`repeatStringNumTimes("*", 8)` should return `"********"` .
2018-10-04 13:37:37 +00:00
2020-11-27 18:02:05 +00:00
```js
assert(repeatStringNumTimes('*', 8) === '********');
```
2018-10-04 13:37:37 +00:00
2020-11-27 18:02:05 +00:00
`repeatStringNumTimes("abc", -2)` should return `""` .
2018-10-04 13:37:37 +00:00
2020-11-27 18:02:05 +00:00
```js
assert(repeatStringNumTimes('abc', -2) === '');
```
2018-10-04 13:37:37 +00:00
2020-11-27 18:02:05 +00:00
The built-in `repeat()` method should not be used.
2018-10-04 13:37:37 +00:00
2020-11-27 18:02:05 +00:00
```js
assert(!/\.repeat/g.test(code));
```
`repeatStringNumTimes("abc", 0)` should return `""` .
```js
assert(repeatStringNumTimes('abc', 0) === '');
```
# --seed--
## --seed-contents--
```js
function repeatStringNumTimes(str, num) {
return str;
}
repeatStringNumTimes("abc", 3);
```
# --solutions--
2018-10-04 13:37:37 +00:00
```js
function repeatStringNumTimes(str, num) {
2019-05-21 01:08:20 +00:00
if (num < 1 ) return ' ' ;
2018-10-04 13:37:37 +00:00
return num === 1 ? str : str + repeatStringNumTimes(str, num-1);
}
repeatStringNumTimes("abc", 3);
```