Skip to content

Commit 3301bc3

Browse files
committed
write test with negative number
1 parent 3ec2172 commit 3301bc3

2 files changed

Lines changed: 22 additions & 14 deletions

File tree

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
function repeatStr(str, num) {
2-
var result = "";
3-
var i;
4-
if (num < 0) {
5-
throw new Error("negative numbers are not valid")
6-
} else if (num === 0) {
7-
result = ""
8-
} else {
9-
for (i = 0; i < num; i++) {
10-
result += str;
11-
}}
12-
return result;
2+
var result = "";
3+
var i;
4+
if (num < 0) {
5+
throw new Error("negative numbers are not valid");
6+
} else if (num === 0) {
7+
result = "";
8+
} else {
9+
for (i = 0; i < num; i++) {
10+
result += str;
11+
}
12+
}
13+
return result;
1314
}
14-
15-
console.log(repeatStr("hello", 44))
15+
16+
//console.log(repeatStr("hello", -1));
17+
//console.log(repeatStr("hello", 0));
18+
//console.log(repeatStr("hello", 5))
1619

1720
module.exports = repeatStr;

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ test("should return just original string", () => {
2727
expect(repeatedStr).toEqual("hello");
2828
});
2929

30-
3130
// Case: Handle count of 0:
3231
// Given a target string `str` and a `count` equal to 0,
3332
// When the repeatStr function is called with these inputs,
@@ -43,3 +42,9 @@ test("should return an empty string for count of 0", () => {
4342
// Given a target string `str` and a negative integer `count`,
4443
// When the repeatStr function is called with these inputs,
4544
// Then it should throw an error, as negative counts are not valid.
45+
test("should throw an error when negative number is entered", () => {
46+
const str = "hello";
47+
const count = -1;
48+
49+
expect(() => repeatStr(str, count)).toThrow("negative numbers are not valid");
50+
});

0 commit comments

Comments
 (0)