mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-06-19 21:09:51 +08:00
Some checks failed
i18n - Build Validation / Validate i18n Builds (20.x) (push) Has been cancelled
CI - Node.js / Lint (20.x) (push) Has been cancelled
CI - Node.js / Build (20.x) (push) Has been cancelled
CI - Node.js / Test (20.x) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (20.x) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 20.x) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 20.x) (push) Has been cancelled
i18n - Download Client UI / Client (push) Has been cancelled
Co-authored-by: Naomi <accounts+github@nhcarrigan.com>
1.8 KiB
1.8 KiB
| id | title | challengeType | forumTopicId | dashedName |
|---|---|---|---|---|
| afcc8d540bea9ea2669306b6 | Repeat a String Repeat a String | 1 | 16041 | 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 ***.
assert.strictEqual(repeatStringNumTimes('*', 3), '***');
repeatStringNumTimes("abc", 3) should return the string abcabcabc.
assert.strictEqual(repeatStringNumTimes('abc', 3), 'abcabcabc');
repeatStringNumTimes("abc", 4) should return the string abcabcabcabc.
assert.strictEqual(repeatStringNumTimes('abc', 4), 'abcabcabcabc');
repeatStringNumTimes("abc", 1) should return the string abc.
assert.strictEqual(repeatStringNumTimes('abc', 1), 'abc');
repeatStringNumTimes("*", 8) should return the string ********.
assert.strictEqual(repeatStringNumTimes('*', 8), '********');
repeatStringNumTimes("abc", -2) should return an empty string ("").
assert.isEmpty(repeatStringNumTimes('abc', -2));
The built-in repeat() method should not be used.
assert.notMatch(__helpers.removeJSComments(code), /\.repeat/g);
repeatStringNumTimes("abc", 0) should return "".
assert.isEmpty(repeatStringNumTimes('abc', 0));
--seed--
--seed-contents--
function repeatStringNumTimes(str, num) {
return str;
}
repeatStringNumTimes('abc', 3);
--solutions--
function repeatStringNumTimes(str, num) {
if (num < 1) return '';
return num === 1 ? str : str + repeatStringNumTimes(str, num - 1);
}
repeatStringNumTimes('abc', 3);