Skip to content

Commit ad2180d

Browse files
committed
updated function repeat() and its tests
1 parent f6314c3 commit ad2180d

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
function repeat(str, count) {
2-
if(count < 0) {
3-
return false;
2+
if (typeof count !== "number" || isNaN(count) || count < 0) {
3+
throw new Error("Count must be a valid number");
44
}
55
return str.repeat(count);
66
}

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,17 @@ test("should repeat the string 0 times ( means the output will be an empty strin
4242
// Given a target string str and a negative integer count,
4343
// When the repeat function is called with these inputs,
4444
// Then it should throw an error or return an appropriate error message, as negative counts are not valid.
45-
test("should repeat the string 0 times ( means the output will be an empty string)", () => {
45+
test("should throw an error if count is not a valid number", () => {
4646
const str = "hello";
47-
const count = -3;
48-
const repeatedStr = repeat(str, count);
49-
expect(repeatedStr).toEqual(false);
50-
});
47+
const count = -8;
48+
expect(() => repeat(str, count)).toThrow("Count must be a valid number");
49+
});
50+
51+
test("should throw an error if count is not a valid number", () => {
52+
const str = "hello";
53+
const count = "abc";
54+
expect(() => repeat(str, count)).toThrow("Count must be a valid number");
55+
});
56+
57+
58+

0 commit comments

Comments
 (0)