Files
wehub-resource-sync dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 11:55:53 +08:00

85 lines
1.8 KiB
Markdown

---
id: afcc8d540bea9ea2669306b6
title: Repeat a String Repeat a String
challengeType: 1
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 the string `***`.
```js
assert.strictEqual(repeatStringNumTimes('*', 3), '***');
```
`repeatStringNumTimes("abc", 3)` should return the string `abcabcabc`.
```js
assert.strictEqual(repeatStringNumTimes('abc', 3), 'abcabcabc');
```
`repeatStringNumTimes("abc", 4)` should return the string `abcabcabcabc`.
```js
assert.strictEqual(repeatStringNumTimes('abc', 4), 'abcabcabcabc');
```
`repeatStringNumTimes("abc", 1)` should return the string `abc`.
```js
assert.strictEqual(repeatStringNumTimes('abc', 1), 'abc');
```
`repeatStringNumTimes("*", 8)` should return the string `********`.
```js
assert.strictEqual(repeatStringNumTimes('*', 8), '********');
```
`repeatStringNumTimes("abc", -2)` should return an empty string (`""`).
```js
assert.isEmpty(repeatStringNumTimes('abc', -2));
```
The built-in `repeat()` method should not be used.
```js
assert.notMatch(__helpers.removeJSComments(code), /\.repeat/g);
```
`repeatStringNumTimes("abc", 0)` should return `""`.
```js
assert.isEmpty(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);
```