We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 0b8375e commit 3876e7fCopy full SHA for 3876e7f
1 file changed
Sprint-3/2-practice-tdd/repeat-str.js
@@ -1,5 +1,26 @@
1
-function repeatStr() {
2
- return "hellohellohello";
+function repeatStr(str, count) {
+ // 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;
24
}
25
26
module.exports = repeatStr;
0 commit comments