Skip to content

Commit 297e964

Browse files
Add tests and implementation for repeat-str
1 parent f05f859 commit 297e964

2 files changed

Lines changed: 23 additions & 7 deletions

File tree

‎practice-tdd/repeat-str.js‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1-
export 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";
5-
}
1+
export function repeatStr(str, count) {
2+
if (count < 0) {
3+
throw new Error("Invalid count: count must not be negative");
4+
}
5+
6+
let result = "";
7+
for (let i = 0; i < count; i++) {
8+
result += str;
9+
}
10+
return result;
11+
}

‎practice-tdd/repeat-str.test.js‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,22 @@ test("should repeat the string count times", () => {
2020
// Given a target string `str` and a `count` equal to 1,
2121
// When the repeatStr function is called with these inputs,
2222
// Then it should return the original `str` without repetition.
23-
23+
test("should return the original string when count is 1", () => {
24+
const str = "hello";
25+
const count = 1;
26+
const repeatedStr = repeatStr(str, count);
27+
expect(repeatedStr).toEqual("hello");
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.
28-
33+
test("should return an empty string when count is 0", () => {
34+
const str = "hello";
35+
const count = 0;
36+
const repeatedStr = repeatStr(str, count);
37+
expect(repeatedStr).toEqual("");
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,

0 commit comments

Comments
 (0)