-
-
Notifications
You must be signed in to change notification settings - Fork 358
Expand file tree
/
Copy pathrepeat-str.js
More file actions
26 lines (22 loc) · 697 Bytes
/
repeat-str.js
File metadata and controls
26 lines (22 loc) · 697 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/**
* Repeats a string a given number of times.
* This re-implements String.prototype.repeat without using it.
*
* @param {string} str - The string to repeat.
* @param {number} count - How many times to repeat it. Must be 0 or greater.
* @returns {string} The string repeated count times, or "" if count is 0.
* @throws {Error} If count is negative.
*
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for
*/
function repeatStr(str, count) {
if (count < 0) {
throw new Error("count must be 0 or greater");
}
let result = "";
for (let i = 1; i < count; i++) {
result = result + str;
}
return result;
}
module.exports = repeatStr;