Skip to content

Commit 4bf763d

Browse files
author
kohanman
committed
Sprint 3 | Coursework 2 - Repeat Str
1 parent 484da5d commit 4bf763d

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
function repeatStr() {
2-
return "hellohellohello";
1+
function repeatStr(str, count) {
2+
if (count < 0) {
3+
return "Error, negative integer";
4+
}
5+
6+
return str.repeat(count);
37
}
48

59
module.exports = repeatStr;

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

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,23 @@ test("should repeat the string count times", () => {
1616
expect(repeatedStr).toEqual("hellohellohello");
1717
});
1818

19-
// case: handle Count of 1:
20-
// Given a target string str and a count equal to 1,
21-
// When the repeatStr function is called with these inputs,
22-
// Then it should return the original str without repetition, ensuring that a count of 1 results in no repetition.
19+
test("should return the original string without repetition", () => {
20+
const str = "hello";
21+
const count = 1;
22+
const repeatedStr = repeatStr(str, count);
23+
expect(repeatedStr).toEqual("hello");
24+
});
2325

24-
// case: Handle Count of 0:
25-
// Given a target string str and a count equal to 0,
26-
// When the repeatStr function is called with these inputs,
27-
// Then it should return an empty string, ensuring that a count of 0 results in an empty output.
26+
test("should return an empty string if count is 0", () => {
27+
const str = "hello";
28+
const count = 0;
29+
const repeatedStr = repeatStr(str, count);
30+
expect(repeatedStr).toEqual("");
31+
});
2832

29-
// case: Negative Count:
30-
// Given a target string str and a negative integer count,
31-
// When the repeatStr function is called with these inputs,
32-
// Then it should throw an error or return an appropriate error message, as negative counts are not valid.
33+
test("should throw an error if count is negative", () => {
34+
const str = "hello";
35+
const count = -1;
36+
const repeatedStr = repeatStr(str, count);
37+
expect(repeatedStr).toEqual("Error, negative integer");
38+
});

0 commit comments

Comments
 (0)