Skip to content

Commit a8355cf

Browse files
committed
repeat task is done
1 parent 4386c93 commit a8355cf

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,29 @@
11
function repeat(str, count) {
2-
if (count < 0) {
2+
if (str === undefined) {
3+
throw new Error("String must be defined");
4+
}
5+
if (str === null || typeof str !== "string") {
6+
throw new Error("String must be a valid string");
7+
}
8+
9+
if (count === undefined || typeof count !== "number") {
10+
throw new Error("Count must be a number");
11+
}
12+
13+
if (count < 0 || !Number.isInteger(count)) {
314
throw new Error("Count must be a non-negative integer");
415
}
516

17+
if (str === "" || count === 0) {
18+
return "";
19+
}
20+
21+
// 5. Repeat the string count times
622
let result = "";
723
for (let i = 0; i < count; i++) {
824
result += str;
925
}
26+
1027
return result;
1128
}
1229

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,38 @@ test("should throw an error when count is negative", () => {
5151
"Count must be a non-negative integer"
5252
);
5353
});
54+
55+
// case: Undefined String
56+
// Given that str is undefined and count is a positive integer,
57+
// When the repeat function is called with these inputs,
58+
// Then it should throw an error or return an appropriate message,
59+
// since repeating an undefined value does not make sense.
60+
61+
test("should throw an error when string is undefined", () => {
62+
const str = undefined;
63+
const count = 2;
64+
expect(() => repeat(str, count)).toThrow("String must be defined");
65+
});
66+
67+
// case: Empty String
68+
// Given an empty string str and a positive integer count,
69+
// When the repeat function is called,
70+
// Then it should return an empty string, since there is nothing to repeat.
71+
72+
test("should return an empty string when input string is empty", () => {
73+
const str = "";
74+
const count = 3;
75+
const repeatedStr = repeat(str, count);
76+
expect(repeatedStr).toEqual("");
77+
});
78+
79+
// case: Null String
80+
// Given that str is null and count is a positive integer,
81+
// When the repeat function is called,
82+
// Then it should throw an error, because null is not a valid string input.
83+
84+
test("should throw an error when string is null", () => {
85+
const str = null;
86+
const count = 2;
87+
expect(() => repeat(str, count)).toThrow("String must be a valid string");
88+
});

0 commit comments

Comments
 (0)