freeCodeCamp/curriculum/challenges/english/02-javascript-algorithms-an.../basic-algorithm-scripting/repeat-a-string-repeat-a-st...

85 lines
1.6 KiB
Markdown
Raw Normal View History

---
id: afcc8d540bea9ea2669306b6
title: Repeat a String Repeat a String
challengeType: 5
forumTopicId: 16041
dashedName: repeat-a-string-repeat-a-string
---
# --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) === '***');
```
`repeatStringNumTimes("abc", 3)` should return `"abcabcabc"`.
```js
assert(repeatStringNumTimes('abc', 3) === 'abcabcabc');
```
`repeatStringNumTimes("abc", 4)` should return `"abcabcabcabc"`.
```js
assert(repeatStringNumTimes('abc', 4) === 'abcabcabcabc');
```
`repeatStringNumTimes("abc", 1)` should return `"abc"`.
```js
assert(repeatStringNumTimes('abc', 1) === 'abc');
```
`repeatStringNumTimes("*", 8)` should return `"********"`.
```js
assert(repeatStringNumTimes('*', 8) === '********');
```
`repeatStringNumTimes("abc", -2)` should return `""`.
```js
assert(repeatStringNumTimes('abc', -2) === '');
```
The built-in `repeat()` method should not be used.
```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--
```js
function repeatStringNumTimes(str, num) {
if (num < 1) return '';
return num === 1 ? str : str + repeatStringNumTimes(str, num-1);
}
repeatStringNumTimes("abc", 3);
```