Skip to content

Commit 256d6f6

Browse files
committed
Implement repeatStr using TDD with edge case handling
1 parent a2c64ac commit 256d6f6

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed
Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
1-
function repeatStr() {
2-
return "hellohellohello";
1+
function repeatStr(str, count) {
2+
if (count < 0) {
3+
throw new Error("Count must be a non-negative integer");
4+
}
5+
6+
let result = "";
7+
8+
for (let i = 0; i < count; i++) {
9+
result += str;
10+
}
11+
12+
return result;
313
}
414

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

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,23 @@ test("should repeat the string count times", () => {
2121
// When the repeatStr function is called with these inputs,
2222
// Then it should return the original `str` without repetition.
2323

24+
test("should return the original string when count is 1", () => {
25+
expect(repeatStr("hello", 1)).toEqual("hello");
26+
});
27+
2428
// Case: Handle count of 0:
2529
// Given a target string `str` and a `count` equal to 0,
2630
// When the repeatStr function is called with these inputs,
2731
// Then it should return an empty string.
2832

33+
test("should return an empty string when count is 0", () => {
34+
expect(repeatStr("hello", 0)).toEqual("");
35+
});
2936
// Case: Handle negative count:
3037
// Given a target string `str` and a negative integer `count`,
3138
// When the repeatStr function is called with these inputs,
3239
// Then it should throw an error, as negative counts are not valid.
40+
41+
test("should throw an error when count is negative", () => {
42+
expect(() => repeatStr("hello", -1)).toThrow();
43+
});

0 commit comments

Comments
 (0)