Skip to content

Commit 3dcd12b

Browse files
committed
repeat-str tests
1 parent 830f9e6 commit 3dcd12b

2 files changed

Lines changed: 33 additions & 4 deletions

File tree

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
1-
function repeatStr() {
2-
return "hellohellohello";
1+
function repeatStr(str, count) {
2+
if (count < 0) {
3+
throw new Error("invalid input: negative number");
4+
}
5+
if (count === 1) {
6+
return str;
7+
}
8+
9+
if (count === 0) {
10+
return "";
11+
}
12+
13+
if (count > 1) {
14+
return str.repeat(count);
15+
}
316
}
417

518
module.exports = repeatStr;

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ const repeatStr = require("./repeat-str");
88
// Given a target string `str` and a positive integer `count` greater than 1,
99
// When the repeatStr function is called with these inputs,
1010
// Then it should return a string that contains the original `str` repeated `count` times.
11-
12-
test("should repeat the string count times", () => {
11+
test("should repeat the string 3 times", () => {
1312
const str = "hello";
1413
const count = 3;
1514
const repeatedStr = repeatStr(str, count);
@@ -20,13 +19,30 @@ test("should repeat the string count times", () => {
2019
// Given a target string `str` and a `count` equal to 1,
2120
// When the repeatStr function is called with these inputs,
2221
// Then it should return the original `str` without repetition.
22+
test("should not repeat string with a count of 1", () => {
23+
const str = "hello";
24+
const count = 1;
25+
const repeatedStr = repeatStr(str, count);
26+
expect(repeatedStr).toEqual("hello");
27+
});
2328

2429
// Case: Handle count of 0:
2530
// Given a target string `str` and a `count` equal to 0,
2631
// When the repeatStr function is called with these inputs,
2732
// Then it should return an empty string.
33+
test("should return empty string with a count of 0", () => {
34+
const str = "hello";
35+
const count = 0;
36+
const repeatedStr = repeatStr(str, count);
37+
expect(repeatedStr).toEqual("");
38+
});
2839

2940
// Case: Handle negative count:
3041
// Given a target string `str` and a negative integer `count`,
3142
// When the repeatStr function is called with these inputs,
3243
// Then it should throw an error, as negative counts are not valid.
44+
test("should throw error when count < 0", () => {
45+
const str = "hello";
46+
const count = -2;
47+
expect(() => repeatStr(str, count)).toThrow();
48+
});

0 commit comments

Comments
 (0)