Skip to content

Commit 8a60a16

Browse files
committed
implemented repeat function and associated tests
1 parent abcb286 commit 8a60a16

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
function repeat() {
2-
return "hellohellohello";
1+
function repeat(str, count) {
2+
if (count < 0) {
3+
return "Count must be a non-negative integer"
4+
}
5+
return str.repeat(count);
36
}
47

58
module.exports = repeat;

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,30 @@ test("should repeat the string count times", () => {
2121
// When the repeat function is called with these inputs,
2222
// Then it should return the original str without repetition, ensuring that a count of 1 results in no repetition.
2323

24+
test("should return original string if count is 1", () => {
25+
const str = "hello";
26+
const count = 1;
27+
expect(repeat(str, count)).toEqual("hello");
28+
});
29+
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.
2834

35+
test("should return empty string if count is 0", () => {
36+
const str = "hello";
37+
const count = 0;
38+
expect(repeat(str, count)).toEqual("");
39+
});
40+
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+
46+
test("should return error message for negative count", () => {
47+
const str = "hello";
48+
const count = -2;
49+
expect(repeat(str, count)).toEqual("Count must be a non-negative integer");
50+
});

0 commit comments

Comments
 (0)