Skip to content

Commit bbc996c

Browse files
author
Payman IB
committed
Refactor repeat function to correctly handle string repetition and maintain error handling for negative counts
1 parent 3585be7 commit bbc996c

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

Sprint-3/2-practice-tdd/repeat.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@ function repeat(str , count) {
44
}
55
if (count === 0) {
66
return "";
7-
}
8-
for (let i = 0; i < count; i++) {
9-
return str.repeat(count);
10-
}
7+
}
8+
9+
let result = "";
10+
for (let i = 0; i < count; i++) {
11+
result += str;
12+
}
13+
14+
return result;
1115
}
12-
console.log(repeat("hello", -2));
16+
console.log(repeat("hello", 2));
1317

1418

1519
module.exports = repeat;

0 commit comments

Comments
 (0)