Skip to content

Commit c7f5ee8

Browse files
committed
Remove unnecessary case checking and manual throwing.
str.repeat() throws Error for values < 0 and return "" for value of -0. Which covers all cases.
1 parent f200ee1 commit c7f5ee8

File tree

2 files changed

+2
-12
lines changed

2 files changed

+2
-12
lines changed
Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,5 @@
11
function repeatStr(str, count) {
2-
if (count < 0) {
3-
throw new Error("invalid input: negative number");
4-
}
5-
6-
if (count === 0) {
7-
return "";
8-
}
9-
10-
if ((count) => 1) {
11-
return str.repeat(count);
12-
}
2+
return str.repeat(count);
133
}
144

155
module.exports = repeatStr;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ test("should return empty string with a count of 0", () => {
4141
// Given a target string `str` and a negative integer `count`,
4242
// When the repeatStr function is called with these inputs,
4343
// Then it should throw an error, as negative counts are not valid.
44-
test("should throw error when count < 0", () => {
44+
test("show error message when count < 0", () => {
4545
const str = "hello";
4646
const count = -2;
4747
expect(() => repeatStr(str, count)).toThrow();

0 commit comments

Comments
 (0)