Skip to content

Commit 3217cc5

Browse files
tests and function to repeat string
1 parent 2c8e627 commit 3217cc5

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed
Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
1-
function repeatStr() {
2-
return "hellohellohello";
1+
function repeatStr(str, count) {
2+
if (count < 0) {
3+
throw new Error("Count must be positive");
4+
}
5+
6+
if (count === 0) {
7+
return " ";
8+
}
9+
10+
return str.repeat(count);
311
}
412

513
module.exports = repeatStr;

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,32 @@ test("should repeat the string count times", () => {
2121
// When the repeatStr function is called with these inputs,
2222
// Then it should return the original `str` without repetition.
2323

24+
test("should repeat the string count times", () => {
25+
const str = "hello";
26+
const count = 1;
27+
const repeatedStr = repeatStr(str, count);
28+
expect(repeatedStr).toEqual("hello");
29+
});
30+
2431
// Case: Handle count of 0:
2532
// Given a target string `str` and a `count` equal to 0,
26-
// When the repeatStr function is called with these inputs,
33+
// When the repeatStr function is called with these inputs
2734
// Then it should return an empty string.
2835

36+
test("should repeat the string count times", () => {
37+
const str = "hello";
38+
const count = 0;
39+
const repeatedStr = repeatStr(str, count);
40+
expect(repeatedStr).toEqual(" ");
41+
});
42+
2943
// Case: Handle negative count:
3044
// Given a target string `str` and a negative integer `count`,
3145
// When the repeatStr function is called with these inputs,
3246
// Then it should throw an error, as negative counts are not valid.
47+
48+
test("should throw an error when count is negative", () => {
49+
const str = "hello";
50+
const count = -3;
51+
expect(() => repeatStr(str, count)).toThrow("Count must be positive");
52+
});

0 commit comments

Comments
 (0)