Skip to content

Commit 2cdf11c

Browse files
committed
implemented repeatStr and added Jest tests
1 parent 5e4a23a commit 2cdf11c

2 files changed

Lines changed: 100 additions & 5 deletions

File tree

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
1-
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+
function repeatStr(str, count) {
2+
if (typeof str !== "string") {
3+
throw new TypeError("First argument must be a string");
4+
}
5+
if (!Number.isInteger(count) || count < 0) {
6+
throw new TypeError("Count must be a non-negative integer");
7+
}
8+
9+
let result = "";
10+
for (let i = 0; i < count; i++) {
11+
result += str;
12+
}
13+
return result;
514
}
615

716
module.exports = repeatStr;

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

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,99 @@ 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 = "world";
25+
const count = 1;
26+
const repeatedStr = repeatStr(str, count);
27+
expect(repeatedStr).toEqual("world");
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.
33+
test("should return an empty string when count is 0", () => {
34+
const str = "test";
35+
const count = 0;
36+
const repeatedStr = repeatStr(str, count);
37+
expect(repeatedStr).toEqual("");
38+
});
2839

2940
// Case: Handle negative count:
3041
// Given a target string `str` and a negative integer `count`,
3142
// When the repeatStr function is called with these inputs,
3243
// Then it should throw an error, as negative counts are not valid.
44+
test("should throw an error for negative count", () => {
45+
const str = "error";
46+
const count = -2;
47+
expect(() => repeatStr(str, count)).toThrow(
48+
"Count must be a non-negative integer"
49+
);
50+
});
51+
52+
test("should throw an error for non-integer count", () => {
53+
const str = "error";
54+
const count = 2.5;
55+
expect(() => repeatStr(str, count)).toThrow(
56+
"Count must be a non-negative integer"
57+
);
58+
});
59+
60+
test("should throw an error for non-string input", () => {
61+
const str = 123;
62+
const count = 3;
63+
expect(() => repeatStr(str, count)).toThrow(
64+
"First argument must be a string"
65+
);
66+
});
67+
68+
test("should throw an error for null or undefined input", () => {
69+
const count = 3;
70+
expect(() => repeatStr(null, count)).toThrow(
71+
"First argument must be a string"
72+
);
73+
expect(() => repeatStr(undefined, count)).toThrow(
74+
"First argument must be a string"
75+
);
76+
});
77+
78+
test("handle empty string input", () => {
79+
const str = "";
80+
const count = 5;
81+
const repeatedStr = repeatStr(str, count);
82+
expect(repeatedStr).toEqual("");
83+
});
84+
85+
test("handle large count input", () => {
86+
const str = "a";
87+
const count = 1000;
88+
const repeatedStr = repeatStr(str, count);
89+
expect(repeatedStr).toEqual("a".repeat(count));
90+
});
91+
92+
test("handle special characters in string", () => {
93+
const str = "!@#";
94+
const count = 4;
95+
const repeatedStr = repeatStr(str, count);
96+
expect(repeatedStr).toEqual("!@#!@#!@#!@#");
97+
});
98+
99+
test("handle whitespace in string", () => {
100+
const str = " ";
101+
const count = 3;
102+
const repeatedStr = repeatStr(str, count);
103+
expect(repeatedStr).toEqual(" ");
104+
});
105+
106+
test("repeats unicode characters correctly", () => {
107+
const str = "😊";
108+
const count = 5;
109+
const repeatedStr = repeatStr(str, count);
110+
expect(repeatedStr).toEqual("😊😊😊😊😊");
111+
});
112+
113+
test("handles empty string", () => {
114+
const str = "";
115+
const count = 5;
116+
const repeatedStr = repeatStr(str, count);
117+
expect(repeatedStr).toEqual("");
118+
});

0 commit comments

Comments
Β (0)