Skip to content

Commit fd74934

Browse files
committed
ensure the count is an integer
1 parent 589841c commit fd74934

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ function repeat(str, count) {
22
if (typeof str !== "string" && typeof str !== "number") {
33
throw new Error("Input should be a string");
44
}
5-
if (Number(count) < 0) {
6-
throw new Error("Count should be a positive number");
5+
if (Number(count) < 0 && Number.isInteger(Number(count))) {
6+
throw new Error("Count should be a positive integer number");
77
}
88
if (typeof count !== "number" && typeof count !== "string") {
9-
throw new Error("Count should be a number");
9+
throw new Error("Count should be a positive integer number");
1010
}
1111
return String(str).repeat(Number(count));
1212
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ test("should return an empty string when count is 0", () => {
4545
test("should throw an error when count is negative", () => {
4646
const str = "hello";
4747
const count = -2;
48-
expect(() => repeat(str, count)).toThrow("Count should be a positive number");
48+
expect(() => repeat(str, count)).toThrow(
49+
"Count should be a positive integer number"
50+
);
4951
});
5052

5153
// case: str is not a string:
@@ -106,5 +108,7 @@ test("should throw an error when str is invalid type", () => {
106108
test("should throw an error when count is an array", () => {
107109
const str = "hello";
108110
const count = [2];
109-
expect(() => repeat(str, count)).toThrow("Count should be a number");
111+
expect(() => repeat(str, count)).toThrow(
112+
"Count should be a positive integer number"
113+
);
110114
});

0 commit comments

Comments
 (0)