Skip to content

Commit 52660c9

Browse files
committed
Editing repeat code
1 parent 73c8fcf commit 52660c9

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
function repeat() {
2-
return "hellohellohello";
2+
const str = arguments[0];
3+
const count = arguments[1];
4+
if (count < 0) {
5+
throw new Error("Count must be a non-negative integer");
6+
}
7+
let result = "";
8+
for (let i = 0; i < count; i++) {
9+
result += str;
10+
}
11+
return result;
312
}
413

14+
515
module.exports = repeat;
16+
console.log(repeat("hello", 3));
17+

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
const repeat = require("./repeat");
33
// Given a target string str and a positive integer count,
44
// When the repeat function is called with these inputs,
5-
// Then it should:
5+
// Then it should: repeat the str count times and return a new string containing the repeated str values.
66

77
// case: repeat String:
88
// Given a target string str and a positive integer count,
@@ -21,12 +21,29 @@ 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 the original string when count is 1", () => {
25+
const str = "hello";
26+
const count = 1;
27+
const repeatedStr = repeat(str, count);
28+
expect(repeatedStr).toEqual("hello");
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 an empty string when count is 0", () => {
36+
const str = "hello";
37+
const count = 0;
38+
const repeatedStr = repeat(str, count);
39+
expect(repeatedStr).toEqual("");
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+
test("should throw an error when count is negative", () => {
46+
const str = "hello";
47+
const count = -2;
48+
expect(() => repeat(str, count)).toThrow("Count must be a non-negative integer");
49+
});

0 commit comments

Comments
 (0)