Skip to content

Commit 94fd6c4

Browse files
committed
repeat - str test - tdd
1 parent aa13124 commit 94fd6c4

2 files changed

Lines changed: 27 additions & 4 deletions

File tree

‎practice-tdd/repeat-str.js‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1-
export function repeatStr() {
1+
export function repeatStr(str, count) {
22
// 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+
return "negative counts are not valid";
6+
}
7+
let result = "";
8+
for (let i = 0; i < count; i++) {
9+
result = result + str;
10+
}
11+
return result;
512
}

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,29 @@ 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 `str` without repetition", () => {
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", () => {
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,
3242
// Then it should throw an error, as negative counts are not valid.
43+
test("should return an error, as negative counts are not valid", () => {
44+
const str = "hello";
45+
const count = -1;
46+
const repeatedStr = repeatStr(str, count);
47+
expect(repeatedStr).toEqual("negative counts are not valid");
48+
});

0 commit comments

Comments
 (0)