--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(repeatStringNumTimes('*', 3) === '***');
repeatStringNumTimes("abc", 3)
should return the string abcabcabc
.
assert(repeatStringNumTimes('abc', 3) === 'abcabcabc');
repeatStringNumTimes("abc", 4)
should return the string abcabcabcabc
.
assert(repeatStringNumTimes('abc', 4) === 'abcabcabcabc');
repeatStringNumTimes("abc", 1)
should return the string abc
.
assert(repeatStringNumTimes('abc', 1) === 'abc');
repeatStringNumTimes("*", 8)
should return the string ********
.
assert(repeatStringNumTimes('*', 8) === '********');
repeatStringNumTimes("abc", -2)
should return an empty string (""
).
assert(repeatStringNumTimes('abc', -2) === '');
The built-in repeat()
method should not be used.
assert(!/\.repeat/g.test(__helpers.removeJSComments(code)));
repeatStringNumTimes("abc", 0)
should return ""
.
assert(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);