Skip to content

Commit 3876e7f

Browse files
committed
Completed repeatStr implementation - passes all 5 Jest test cases
1 parent 0b8375e commit 3876e7f

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed
Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,26 @@
1-
function repeatStr() {
2-
return "hellohellohello";
1+
function repeatStr(str, count) {
2+
// Reject negative counts - not valid for repetition
3+
if (count < 0) {
4+
throw new Error("Count cannot be negative");
5+
}
6+
7+
// If count is 0, return empty string immediately
8+
if (count === 0) {
9+
return "";
10+
}
11+
12+
// For count = 1, just return the original string
13+
if (count === 1) {
14+
return str;
15+
}
16+
17+
// For count > 1: build the repeated string using a loop
18+
let result = "";
19+
for (let i = 0; i < count; i++) {
20+
result += str;
21+
}
22+
23+
return result;
324
}
425

526
module.exports = repeatStr;

0 commit comments

Comments
 (0)