Skip to content

Commit 514a13e

Browse files
committed
Implement repeatStr with tests
1 parent a57e153 commit 514a13e

2 files changed

Lines changed: 39 additions & 11 deletions

File tree

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1-
function repeatStr() {
2-
// Your implementation of this function must *not* call String.prototype.repeat (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat).
1+
function repeatStr(str, count) {
2+
// Your implementation of this function must not call String.prototype.repeat (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat)
33
// The goal is to re-implement that function, not to use it.
4-
return "hellohellohello";
4+
if (count < 0) {
5+
throw new Error("count must not be negative");
6+
}
7+
8+
let result = "";
9+
for (let i = 0; i < count; i++) {
10+
result += str;
11+
}
12+
return result;
513
}
614

7-
module.exports = repeatStr;
15+
module.exports = repeatStr;
Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
// Implement a function repeatStr
22
const repeatStr = require("./repeat-str");
3-
// Given a target string `str` and a positive integer `count`,
3+
// Given a target string str and a positive integer count,
44
// When the repeatStr function is called with these inputs,
55
// Then it should:
66

77
// Case: handle multiple repetitions:
8-
// Given a target string `str` and a positive integer `count` greater than 1,
8+
// Given a target string str and a positive integer count greater than 1,
99
// When the repeatStr function is called with these inputs,
10-
// Then it should return a string that contains the original `str` repeated `count` times.
10+
// Then it should return a string that contains the original str repeated count times.
1111

1212
test("should repeat the string count times", () => {
1313
const str = "hello";
@@ -17,16 +17,36 @@ test("should repeat the string count times", () => {
1717
});
1818

1919
// Case: handle count of 1:
20-
// Given a target string `str` and a `count` equal to 1,
20+
// Given a target string str and a count equal to 1,
2121
// When the repeatStr function is called with these inputs,
22-
// Then it should return the original `str` without repetition.
22+
// Then it should return the original str without repetition.
23+
24+
test("should return the original string when count is 1", () => {
25+
const str = "hello";
26+
const count = 1;
27+
const repeatedStr = repeatStr(str, count);
28+
expect(repeatedStr).toEqual("hello");
29+
});
2330

2431
// Case: Handle count of 0:
25-
// Given a target string `str` and a `count` equal to 0,
32+
// Given a target string str and a count equal to 0,
2633
// When the repeatStr function is called with these inputs,
2734
// Then it should return an empty string.
2835

36+
test("should return an empty string when count is 0", () => {
37+
const str = "hello";
38+
const count = 0;
39+
const repeatedStr = repeatStr(str, count);
40+
expect(repeatedStr).toEqual("");
41+
});
42+
2943
// Case: Handle negative count:
30-
// Given a target string `str` and a negative integer `count`,
44+
// Given a target string str and a negative integer count,
3145
// When the repeatStr function is called with these inputs,
3246
// Then it should throw an error, as negative counts are not valid.
47+
48+
test("should throw an error when count is negative", () => {
49+
const str = "hello";
50+
const count = -1;
51+
expect(() => repeatStr(str, count)).toThrow();
52+
});

0 commit comments

Comments
 (0)