Skip to content

Commit 0b70ab5

Browse files
committed
error thrown issue is fixed
1 parent 6936466 commit 0b70ab5

File tree

2 files changed

+8
-11
lines changed

2 files changed

+8
-11
lines changed
Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
function repeatStr(str, count) {
2-
try {
3-
if (count === 0) return "";
4-
else if (count === 1) return str;
5-
else if (count < 0) throw new Error("an error is thrown");
6-
else return str.repeat(count);
7-
} catch (e) {
8-
return e.message;
9-
}
2+
if (count === 0) return "";
3+
else if (count === 1) return str;
4+
else if (count < 0) throw new Error("an error is thrown");
5+
else return str.repeat(count);
106
}
117

128
module.exports = repeatStr;

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,10 @@ test("should return an empty string as the count is 0", () => {
4242
// Given a target string `str` and a negative integer `count`,
4343
// When the repeatStr function is called with these inputs,
4444
// Then it should throw an error, as negative counts are not valid.
45-
test("should repeat the string count times", () => {
45+
test("should throw an error when count is a negative number", () => {
4646
const str = "hello";
4747
const count = -2;
48-
const repeatedStr = repeatStr(str, count);
49-
expect(repeatedStr).toEqual("an error is thrown");
48+
expect(() => {
49+
repeatStr(str, count);
50+
}).toThrow();
5051
});

0 commit comments

Comments
 (0)