curriculum/challenges/english/blocks/lab-repeat-a-string/afcc8d540bea9ea2669306b6.md
In this lab, you will create a function that repeats a given string a specific number of times. For the purpose of this lab, do not use the built-in .repeat() method.
Objective: Fulfill the user stories below and get all the tests to pass to complete the lab.
User Stories:
repeatStringNumTimes that takes two parameters: a string and a number.You should create a function named repeatStringNumTimes.
assert.isFunction(repeatStringNumTimes);
repeatStringNumTimes should take two parameters.
assert.lengthOf(repeatStringNumTimes, 2);
The function repeatStringNumTimes should always return a string.
assert.isString(repeatStringNumTimes('hello', 3));
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));
repeatStringNumTimes("abc", 0) should return "".
assert.isEmpty(repeatStringNumTimes('abc', 0));
The built-in repeat() method should not be used.
assert.notMatch(__helpers.removeJSComments(code), /\.repeat/g);
function repeatStringNumTimes(str, num) {
if (num < 1) return '';
return num === 1 ? str : str + repeatStringNumTimes(str, num - 1);
}
repeatStringNumTimes('abc', 3);