Skip to content

Commit 21c6130

Browse files
repeat string implemented and tests written
1 parent 3adb2de commit 21c6130

2 files changed

Lines changed: 47 additions & 4 deletions

File tree

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1-
function repeatStr() {
1+
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 str.repeat(count);
6+
}
7+
else{
8+
throw new Error("Invalid count");
9+
}
510
}
611

712
module.exports = repeatStr;

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

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,56 @@ test("should repeat the string count times", () => {
1515
const repeatedStr = repeatStr(str, count);
1616
expect(repeatedStr).toEqual("hellohellohello");
1717
});
18+
test("should repeat the string count times", () => {
19+
const str = "codeyourfuture";
20+
const count = 2;
21+
const repeatedStr = repeatStr(str, count);
22+
expect(repeatedStr).toEqual("codeyourfuturecodeyourfuture");
23+
});
1824

1925
// Case: handle count of 1:
2026
// Given a target string `str` and a `count` equal to 1,
2127
// When the repeatStr function is called with these inputs,
2228
// Then it should return the original `str` without repetition.
23-
29+
test("should repeat the string count times", () => {
30+
const str = "hello";
31+
const count = 1;
32+
const repeatedStr = repeatStr(str, count);
33+
expect(repeatedStr).toEqual("hello");
34+
});
35+
test("should repeat the string count times", () => {
36+
const str = "codeyourfuture";
37+
const count = 1;
38+
const repeatedStr = repeatStr(str, count);
39+
expect(repeatedStr).toEqual("codeyourfuture");
40+
});
2441
// Case: Handle count of 0:
2542
// Given a target string `str` and a `count` equal to 0,
2643
// When the repeatStr function is called with these inputs,
2744
// Then it should return an empty string.
28-
45+
test("should repeat the string count times", () => {
46+
const str = "hello";
47+
const count = 0;
48+
const repeatedStr = repeatStr(str, count);
49+
expect(repeatedStr).toEqual("");
50+
});
51+
test("should repeat the string count times", () => {
52+
const str = "codeyourfuture";
53+
const count = 0;
54+
const repeatedStr = repeatStr(str, count);
55+
expect(repeatedStr).toEqual("");
56+
});
2957
// Case: Handle negative count:
3058
// Given a target string `str` and a negative integer `count`,
3159
// When the repeatStr function is called with these inputs,
3260
// Then it should throw an error, as negative counts are not valid.
61+
test("should repeat the string count times", () => {
62+
const str = "hello";
63+
const count = -4;
64+
expect(() => repeatStr(str, count)).toThrow("Invalid count");
65+
});
66+
test("should repeat the string count times", () => {
67+
const str = "codeyourfuture";
68+
const count = -1;
69+
expect(() => repeatStr(str, count)).toThrow("Invalid count");
70+
});

0 commit comments

Comments
 (0)