Skip to content

Commit 49def27

Browse files
committed
-fixing the function repeat -fix the repeat-str test
1 parent 3fd455a commit 49def27

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
function repeatStr(word, times) {
2-
if (times > 0) {
3-
return word.repeat(times);
4-
} else if (times < 0) {
5-
return "Error:negative number not allowed";
6-
} else {
2+
if (times < 0) {
3+
throw new Error("negative number is not allowed");
4+
} else if (times === 0) {
75
return "";
6+
} else {
7+
return word.repeat(times);
88
}
99
}
1010
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,7 +42,8 @@ test("should return an empty string when the number of times is 0", () => {
4242
// Then it should throw an error, as negative counts are not valid.
4343
test("should return a string 'negative number not allowed' when negative number passed", () => {
4444
let word = "hello";
45-
let times = -3;
46-
const repeatedStr = repeatStr(word, times);
47-
expect(repeatedStr).toBe("Error:negative number not allowed");
45+
let times = -1;
46+
expect(() => {
47+
repeatStr("hello", -1);
48+
}).toThrow("negative number is not allowed");
4849
});

0 commit comments

Comments
 (0)