Skip to content

Commit 9722cde

Browse files
committed
Refactor repeatStr function to handle negative counts and return repeated string
1 parent 150a497 commit 9722cde

File tree

2 files changed

+22
-20
lines changed

2 files changed

+22
-20
lines changed
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
function repeatStr() {
2-
return "hellohellohello";
1+
function repeatStr(str, count) {
2+
if (count < 0) {
3+
throw new Error("Count cannot be negative");
4+
}
5+
return str.repeat(count);
36
}
47

58
module.exports = repeatStr;
Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,31 @@
1-
// Implement a function repeatStr
2-
const repeatStr = require("./repeat-str");
3-
// Given a target string str and a positive integer count,
4-
// When the repeatStr 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 repeatStr 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.
11-
12-
test("should repeat the string count times", () => {
13-
const str = "hello";
14-
const count = 3;
15-
const repeatedStr = repeatStr(str, count);
16-
expect(repeatedStr).toEqual("hellohellohello");
17-
});
18-
191
// case: handle Count of 1:
202
// Given a target string str and a count equal to 1,
213
// When the repeatStr function is called with these inputs,
224
// Then it should return the original str without repetition, ensuring that a count of 1 results in no repetition.
5+
test("should return original string when count is 1", () => {
6+
const str = "hello";
7+
const count = 1;
8+
const repeatedStr = repeatStr(str, count);
9+
expect(repeatedStr).toEqual("hello");
10+
});
2311

2412
// case: Handle Count of 0:
2513
// Given a target string str and a count equal to 0,
2614
// When the repeatStr function is called with these inputs,
2715
// Then it should return an empty string, ensuring that a count of 0 results in an empty output.
16+
test("should return empty string when count is 0", () => {
17+
const str = "hello";
18+
const count = 0;
19+
const repeatedStr = repeatStr(str, count);
20+
expect(repeatedStr).toEqual("");
21+
});
2822

2923
// case: Negative Count:
3024
// Given a target string str and a negative integer count,
3125
// When the repeatStr function is called with these inputs,
3226
// Then it should throw an error or return an appropriate error message, as negative counts are not valid.
27+
test("should throw an error for negative count", () => {
28+
const str = "hello";
29+
const count = -3;
30+
expect(() => repeatStr(str, count)).toThrow();
31+
});

0 commit comments

Comments
 (0)