Skip to content

Commit 3d1dcea

Browse files
committed
feat(sprint-3): implement repeat function with TDD
1 parent ef0c3ec commit 3d1dcea

File tree

2 files changed

+31
-27
lines changed

2 files changed

+31
-27
lines changed

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1-
function repeat() {
2-
return "hellohellohello";
1+
function repeat(str, count) {
2+
if (count < 0) throw new Error("Count cannot be negative");
3+
if (count === 0) return "";
4+
5+
let result = "";
6+
for (let i = 0; i < count; i++) {
7+
result += str;
8+
}
9+
return result;
310
}
411

512
module.exports = repeat;
Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,29 @@
1-
// Implement a function repeat
21
const repeat = require("./repeat");
3-
// Given a target string str and a positive integer count,
4-
// When the repeat function is called with these inputs,
5-
// Then it should:
6-
7-
// case: repeat String:
8-
// Given a target string str and a positive integer count,
9-
// When the repeat function is called with these inputs,
10-
// Then it should repeat the str count times and return a new string containing the repeated str values.
112

123
test("should repeat the string count times", () => {
13-
const str = "hello";
14-
const count = 3;
15-
const repeatedStr = repeat(str, count);
16-
expect(repeatedStr).toEqual("hellohellohello");
4+
expect(repeat("hello", 3)).toEqual("hellohellohello");
5+
});
6+
7+
test("should return the original string when count is 1", () => {
8+
expect(repeat("hello", 1)).toEqual("hello");
9+
});
10+
11+
test("should return an empty string when count is 0", () => {
12+
expect(repeat("hello", 0)).toEqual("");
1713
});
1814

19-
// case: handle Count of 1:
20-
// Given a target string str and a count equal to 1,
21-
// 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.
15+
test("should throw an error when count is negative", () => {
16+
expect(() => repeat("hello", -1)).toThrow("Count cannot be negative");
17+
});
2318

24-
// case: Handle Count of 0:
25-
// Given a target string str and a count equal to 0,
26-
// When the repeat function is called with these inputs,
27-
// Then it should return an empty string, ensuring that a count of 0 results in an empty output.
19+
test("should repeat string multiple times", () => {
20+
expect(repeat("abc", 4)).toEqual("abcabcabcabc");
21+
});
2822

29-
// case: Negative Count:
30-
// Given a target string str and a negative integer count,
31-
// When the repeat function is called with these inputs,
32-
// Then it should throw an error or return an appropriate error message, as negative counts are not valid.
23+
test("should handle empty string", () => {
24+
expect(repeat("", 5)).toEqual("");
25+
});
26+
27+
test("should handle single character", () => {
28+
expect(repeat("x", 5)).toEqual("xxxxx");
29+
});

0 commit comments

Comments
 (0)