Skip to content

Commit 8dff45a

Browse files
committed
repeat-str.js committed
1 parent 64e0ddd commit 8dff45a

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,44 @@
1+
/**
2+
*
13
function repeatStr() {
24
return "hellohellohello";
35
}
46
57
module.exports = repeatStr;
8+
*
9+
*/
10+
11+
// repeat-str.js
12+
13+
function repeatStr(str, count) {
14+
// Convert count to a number
15+
const numCount = Number(count);
16+
17+
// Check if count is a valid number
18+
if (isNaN(numCount)) {
19+
throw new Error("Count must be a number");
20+
}
21+
22+
// Check for negative count
23+
if (numCount < 0) {
24+
throw new Error("Count must be a positive integer");
25+
}
26+
27+
// Handle count of 0
28+
if (numCount === 0) {
29+
return "";
30+
}
31+
32+
// Floor the count to handle decimal numbers
33+
const repeatTimes = Math.floor(numCount);
34+
let result = "";
35+
36+
// Build the repeated string
37+
for (let i = 0; i < repeatTimes; i++) {
38+
result += str;
39+
}
40+
41+
return result;
42+
}
43+
44+
module.exports = repeatStr;

0 commit comments

Comments
 (0)