2018-10-10 18:03:03 -04:00
---
id: afcc8d540bea9ea2669306b6
2021-02-06 04:42:36 +00:00
title: Repeat a String Repeat a String
2018-10-10 18:03:03 -04:00
challengeType: 5
2021-01-12 08:18:51 -08:00
forumTopicId: 16041
2021-01-13 03:31:00 +01:00
dashedName: repeat-a-string-repeat-a-string
2018-10-10 18:03:03 -04:00
---
2020-12-16 00:37:30 -07:00
# --description--
2021-02-06 04:42:36 +00:00
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.
2021-01-12 08:18:51 -08:00
2020-12-16 00:37:30 -07:00
# --hints--
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
`repeatStringNumTimes("*", 3)` should return `"***"` .
2020-12-16 00:37:30 -07:00
```js
assert(repeatStringNumTimes('*', 3) === '***');
2018-10-10 18:03:03 -04:00
```
2021-02-06 04:42:36 +00:00
`repeatStringNumTimes("abc", 3)` should return `"abcabcabc"` .
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
assert(repeatStringNumTimes('abc', 3) === 'abcabcabc');
```
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
`repeatStringNumTimes("abc", 4)` should return `"abcabcabcabc"` .
2018-10-10 18:03:03 -04:00
```js
2020-12-16 00:37:30 -07:00
assert(repeatStringNumTimes('abc', 4) === 'abcabcabcabc');
```
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
`repeatStringNumTimes("abc", 1)` should return `"abc"` .
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
assert(repeatStringNumTimes('abc', 1) === 'abc');
2018-10-10 18:03:03 -04:00
```
2021-02-06 04:42:36 +00:00
`repeatStringNumTimes("*", 8)` should return `"********"` .
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
assert(repeatStringNumTimes('*', 8) === '********');
```
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
`repeatStringNumTimes("abc", -2)` should return `""` .
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
assert(repeatStringNumTimes('abc', -2) === '');
```
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
The built-in `repeat()` method should not be used.
2018-10-10 18:03:03 -04:00
```js
2020-12-16 00:37:30 -07:00
assert(!/\.repeat/g.test(code));
2018-10-10 18:03:03 -04:00
```
2020-08-13 17:24:35 +02:00
2021-02-06 04:42:36 +00:00
`repeatStringNumTimes("abc", 0)` should return `""` .
2021-01-12 08:18:51 -08:00
```js
assert(repeatStringNumTimes('abc', 0) === '');
```
2021-01-13 03:31:00 +01:00
# --seed--
## --seed-contents--
```js
function repeatStringNumTimes(str, num) {
return str;
}
repeatStringNumTimes("abc", 3);
```
2020-12-16 00:37:30 -07:00
# --solutions--
2021-01-13 03:31:00 +01:00
```js
function repeatStringNumTimes(str, num) {
if (num < 1 ) return ' ' ;
return num === 1 ? str : str + repeatStringNumTimes(str, num-1);
}
repeatStringNumTimes("abc", 3);
```