Skip to content

Commit ba2b1c2

Browse files
committed
repeat-str.js + tests completed
1 parent e75c3f6 commit ba2b1c2

2 files changed

Lines changed: 17 additions & 4 deletions

File tree

‎practice-tdd/repeat-str.js‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
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+
return str.repeat(count);
53
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,28 @@ 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+
test("should return original str value if called once", () => {
24+
const str = "hello";
25+
const count = 1;
26+
expect(repeatStr(str, count)).toEqual(str);
27+
});
2328

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.
33+
test("should return empty string if called with count =0", () => {
34+
const str = "hello";
35+
const count = 0;
36+
expect(repeatStr(str, count)).toEqual("");
37+
});
2838

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 throw error as negatives are not valid", () => {
44+
const str = "hello";
45+
const count = -1;
46+
expect(() => repeatStr(str, count)).toThrow(Error);
47+
});

0 commit comments

Comments
 (0)