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
2021-01-13 03:31:00 +01:00
dashedName: repeat-a-string-repeat-a-string
2018-10-04 14:37:37 +01:00
---
2020-11-27 19:02:05 +01: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 14:37:37 +01:00
```
2020-11-27 19:02:05 +01:00
`repeatStringNumTimes("abc", 3)` should return `"abcabcabc"` .
2018-10-04 14:37:37 +01:00
2020-11-27 19:02:05 +01:00
```js
assert(repeatStringNumTimes('abc', 3) === 'abcabcabc');
```
2018-10-04 14:37:37 +01:00
2020-11-27 19:02:05 +01:00
`repeatStringNumTimes("abc", 4)` should return `"abcabcabcabc"` .
2018-10-04 14:37:37 +01:00
```js
2020-11-27 19:02:05 +01:00
assert(repeatStringNumTimes('abc', 4) === 'abcabcabcabc');
```
2018-10-04 14:37:37 +01:00
2020-11-27 19:02:05 +01:00
`repeatStringNumTimes("abc", 1)` should return `"abc"` .
```js
assert(repeatStringNumTimes('abc', 1) === 'abc');
2018-10-04 14:37:37 +01:00
```
2020-11-27 19:02:05 +01:00
`repeatStringNumTimes("*", 8)` should return `"********"` .
2018-10-04 14:37:37 +01:00
2020-11-27 19:02:05 +01:00
```js
assert(repeatStringNumTimes('*', 8) === '********');
```
2018-10-04 14:37:37 +01:00
2020-11-27 19:02:05 +01:00
`repeatStringNumTimes("abc", -2)` should return `""` .
2018-10-04 14:37:37 +01:00
2020-11-27 19:02:05 +01:00
```js
assert(repeatStringNumTimes('abc', -2) === '');
```
2018-10-04 14:37:37 +01:00
2020-11-27 19:02:05 +01:00
The built-in `repeat()` method should not be used.
2018-10-04 14:37:37 +01:00
2020-11-27 19:02:05 +01: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 14:37:37 +01:00
```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);
```