Skip to content

Commit 45dc764

Browse files
committed
2-practice-tdd/repeat test
1 parent 47e385f commit 45dc764

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
1-
function repeat() {
2-
return "hellohellohello";
1+
function repeat(str, count) {
2+
// Handle edge cases
3+
if (count < 0) {
4+
throw new Error("Count cannot be negative");
5+
}
6+
7+
if (count === 0) {
8+
return "";
9+
}
10+
11+
// Build the repeated string
12+
let result = "";
13+
for (let i = 0; i < count; i++) {
14+
result += str;
15+
}
16+
17+
return result;
318
}
419

520
module.exports = repeat;

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,31 @@ test("should repeat the string count times", () => {
1919
// case: handle Count of 1:
2020
// Given a target string str and a count equal to 1,
2121
// When the repeat function is called with these inputs,
22-
// Then it should return the original str without repetition, ensuring that a count of 1 results in no repetition.
22+
// Then it should return the original str without repetition, ensuring that a count of 1 results in no repetition
23+
test("should return the original string for count of 1", () => {
24+
const str = "hello";
25+
const count = 1;
26+
const repeatedStr = repeat(str, count);
27+
expect(repeatedStr).toEqual("hello");
28+
});
2329

2430
// case: Handle Count of 0:
2531
// Given a target string str and a count equal to 0,
2632
// When the repeat function is called with these inputs,
2733
// Then it should return an empty string, ensuring that a count of 0 results in an empty output.
34+
test("should return an empty string for count of 0", () => {
35+
const str = "hello";
36+
const count = 0;
37+
const repeatedStr = repeat(str, count);
38+
expect(repeatedStr).toEqual("");
39+
});
2840

2941
// case: Negative Count:
3042
// Given a target string str and a negative integer count,
3143
// When the repeat function is called with these inputs,
3244
// Then it should throw an error or return an appropriate error message, as negative counts are not valid.
45+
test("should throw an error for negative count", () => {
46+
const str = "hello";
47+
const count = -2;
48+
expect(() => repeat(str, count)).toThrow("Count cannot be negative");
49+
});

0 commit comments

Comments
 (0)