Skip to content

Commit 2002729

Browse files
added more tests for repeat-str-tests
1 parent ce61555 commit 2002729

2 files changed

Lines changed: 29 additions & 5 deletions

File tree

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
// 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).
22
// The goal is to re-implement that function, not to use it.
33
function repeatStr(str, count) {
4-
if (typeof str !== "string" || typeof count !== "number" || count < 0) {
5-
throw new Error(
6-
"Invalid input: str must be a string and count must be a non-negative integer"
7-
);
4+
if (typeof str !== "string") {
5+
return "";
6+
}
7+
if (count < 0) {
8+
throw new Error("Invalid input: count must be a non-negative integer");
89
}
910

1011
let result = "";
@@ -15,5 +16,13 @@ function repeatStr(str, count) {
1516

1617
return result;
1718
}
19+
console.log(repeatStr("hello", 3)); // Output: "hellohellohello"
20+
console.log(repeatStr("fella", 1)); // Output: "fella"
21+
console.log(repeatStr("fella", 0)); // Output: ""
22+
console.log(repeatStr("ab", 3)); // Output: "ababab"
23+
console.log(repeatStr("a", 1000).length); // Output: 1000
24+
console.log(repeatStr("", 5)); // Output: ""
25+
console.log(repeatStr([], 3)); // Output: ""
26+
console.log(repeatStr(null, 3)); // Output: ""
1827

1928
module.exports = repeatStr;

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,23 @@ test("should repeat the string count of 0", () => {
4242

4343
// Case: Handle negative count:
4444
test(`should throw an error for negative count`, () => {
45-
expect(() => repeatStr("fella", -2)).toThrowError();
45+
expect(() => repeatStr("fella", -2)).toThrowError(
46+
"Invalid input: count must be a non-negative integer"
47+
);
4648
});
4749
// Given a target string `str` and a negative integer `count`,
4850
// When the repeatStr function is called with these inputs,
4951
// Then it should throw an error, as negative counts are not valid.
52+
test("should return empty string for non-string input", () => {
53+
expect(repeatStr([], 3)).toEqual("");
54+
expect(repeatStr(null, 3)).toEqual("");
55+
});
56+
test("should handle large repeat counts", () => {
57+
expect(repeatStr("a", 1000).length).toEqual(1000);
58+
});
59+
test("should repeat a multi-character string multiple times", () => {
60+
expect(repeatStr("ab", 3)).toEqual("ababab");
61+
});
62+
test("should return an empty string when repeating an empty string", () => {
63+
expect(repeatStr("", 5)).toEqual("");
64+
});

0 commit comments

Comments
 (0)