Skip to content

Commit fb18946

Browse files
committed
Completed repeat exercise
1 parent 7536db7 commit fb18946

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
1-
function repeat() {
2-
return "hellohellohello";
1+
function repeat(string, count) {
2+
if (isNaN(count)) {
3+
throw new Error("count is not a number");
4+
} else if (count < 0) {
5+
throw new Error("negative numbers are not valid");
6+
} else if (count === 0) {
7+
return "";
8+
} else if (count === 1) {
9+
return string;
10+
} else {
11+
return string.repeat(count);
12+
}
313
}
414

515
module.exports = repeat;

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,35 @@ test("should repeat the string count times", () => {
2121
// When the repeat function is called with these inputs,
2222
// Then it should return the original str without repetition, ensuring that a count of 1 results in no repetition.
2323

24+
test("should repeat the string 1 times", () => {
25+
const str = "Hello, World!";
26+
const count = 1;
27+
const repeatedStr = repeat(str, count);
28+
expect(repeatedStr).toEqual(str);
29+
});
30+
2431
// case: Handle Count of 0:
2532
// Given a target string str and a count equal to 0,
2633
// When the repeat function is called with these inputs,
2734
// Then it should return an empty string, ensuring that a count of 0 results in an empty output.
2835

36+
test("should return an empty string for 0 times", () => {
37+
const str = "Bye, World!";
38+
const count = 0;
39+
const repeatedStr = repeat(str, count);
40+
expect(repeatedStr).toEqual("");
41+
});
42+
2943
// case: Negative Count:
3044
// Given a target string str and a negative integer count,
3145
// When the repeat function is called with these inputs,
3246
// Then it should throw an error or return an appropriate error message, as negative counts are not valid.
47+
48+
test("should throw an error for a negative number", () => {
49+
expect(() => repeat("Ello!", -1)).toThrow("negative numbers are not valid");
50+
});
51+
52+
// case: count is not a number:
53+
test("should throw an error when count is not a number", () => {
54+
expect(() => repeat("Hello?", "@")).toThrow("count is not a number");
55+
});

0 commit comments

Comments
 (0)