Skip to content

Commit c3cd1f3

Browse files
committed
Add repeatStr tests and implement function to satisfy them
1 parent 92a19ee commit c3cd1f3

2 files changed

Lines changed: 27 additions & 4 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).
3-
// The goal is to re-implement that function, not to use it.
4-
return "hellohellohello";
1+
function repeatStr(str, count) {
2+
if (count < 0) {
3+
throw new Error("Count cannot be negative");
4+
}
5+
6+
let result = "";
7+
8+
for (let i = 0; i < count; i++) {
9+
result += str;
10+
}
11+
12+
return result;
513
}
614

715
module.exports = repeatStr;

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,27 @@ test("should repeat the string count times", () => {
2121
// When the repeatStr function is called with these inputs,
2222
// Then it should return the original `str` without repetition.
2323

24+
test("should return the original string when count is 1", () => {
25+
expect(repeatStr("hello", 1)).toEqual("hello");
26+
});
27+
28+
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.
2833

34+
test("should return an empty string when count is 0", () => {
35+
expect(repeatStr("hello", 0)).toEqual("");
36+
});
37+
38+
2939
// Case: Handle negative count:
3040
// Given a target string `str` and a negative integer `count`,
3141
// When the repeatStr function is called with these inputs,
3242
// Then it should throw an error, as negative counts are not valid.
43+
44+
test("should throw an error for negative count", () => {
45+
expect(() => repeatStr("hello", -1)).toThrow();
46+
});
47+

0 commit comments

Comments
 (0)