Skip to content

Commit a074c86

Browse files
Implement repeatStr function
1 parent 34c5ab9 commit a074c86

2 files changed

Lines changed: 23 additions & 4 deletions

File tree

‎practice-tdd/repeat-str.js‎

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

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

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

19+
test("should return the original string when count is 1", () => {
20+
const str = "hello";
21+
const count = 1;
22+
const repeatedStr = repeatStr(str, count);
23+
expect(repeatedStr).toEqual("hello");
24+
});
25+
26+
test("should return an empty string when count is 0", () => {
27+
const str = "hello";
28+
const count = 0;
29+
const repeatedStr = repeatStr(str, count);
30+
expect(repeatedStr).toEqual("");
31+
});
32+
1933
// Case: handle count of 1:
2034
// Given a target string `str` and a `count` equal to 1,
2135
// When the repeatStr function is called with these inputs,

0 commit comments

Comments
 (0)