-
-
Notifications
You must be signed in to change notification settings - Fork 336
Expand file tree
/
Copy pathrepeat-str.js
More file actions
44 lines (36 loc) · 898 Bytes
/
repeat-str.js
File metadata and controls
44 lines (36 loc) · 898 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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/**
* Origina file
*
function repeatStr() {
return "hellohellohello";
}
module.exports = repeatStr;
*
* End of file
*/
/**
* @param {string} str - The string to repeat
* @param {number} count - The number of times to repeat the string (must be non-negative)
* @returns {string} The repeated string
* @throws {Error} When count is negative
*/
function repeatStr(str, count) {
// Input validation
if (typeof str !== 'string') {
throw new Error('First argument must be a string');
}
if (typeof count !== 'number' || !Number.isInteger(count)) {
throw new Error('Count must be an integer');
}
// Handle negative count
if (count < 0) {
throw new Error('Count must be a positive integer');
}
// Handle count of 0
if (count === 0) {
return "";
}
// Use built-in repeat method for efficiency
return str.repeat(count);
}
module.exports = repeatStr;