Skip to content

Commit 3eec672

Browse files
wrote a functione condition to handle test case for negative count
1 parent 8b6b488 commit 3eec672

2 files changed

Lines changed: 4 additions & 1 deletion

File tree

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ function repeatStr(str,count) {
33
for (let i = 0; i < count; i++) {
44
result += str;
55
}
6+
if (count < 0) {
7+
throw new Error("Count must be a non-negative integer");
8+
}
69
return result;
710
// Your implementation of this function must *not* call String.prototype.repeat (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat).
811
// The goal is to re-implement that function, not to use it.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,5 @@ test("should return an empty string when count is 0", () => {
4848
test("should throw an error when count is negative", () => {
4949
const str = "hello";
5050
const count = -1;
51-
expect(() => repeatStr(str, count)).toThrow();
51+
expect(() => repeatStr(str, count)).toThrow("Count must be a non-negative integer");
5252
});

0 commit comments

Comments
 (0)