Skip to content

Commit a36b127

Browse files
committed
implemented string repetition with validation for count
1 parent 0f8bc3b commit a36b127

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed
Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
1-
function repeatStr() {
2-
return "hellohellohello";
1+
function repeatStr(str, count) {
2+
if (count < 0) {
3+
throw new Error("Count cannot be negative");
4+
}
5+
6+
if (count === 0) {
7+
return "";
8+
}
9+
10+
if (count === 1) {
11+
return str;
12+
}
13+
14+
let result = "";
15+
for (let i = 0; i < count; i++) {
16+
result += str;
17+
}
18+
19+
return result;
320
}
421

5-
module.exports = repeatStr;
22+
module.exports = repeatStr;

0 commit comments

Comments
 (0)