Skip to content

Commit 2a87e48

Browse files
committed
Add another tests for count test and change the function for repeat-str.
1 parent 914b810 commit 2a87e48

3 files changed

Lines changed: 67 additions & 21 deletions

File tree

Sprint-3/2-practice-tdd/count.test.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,50 @@ test("should return 0 when no occurrences of a character are found", () => {
3636
const count = countChar(str, char);
3737
expect(count).toEqual(0);
3838
});
39+
40+
// Scenario: Empty String or Space
41+
// Given the input string `str`,
42+
// And a character `char` that is an empty string or a space.
43+
// When the function is called with these inputs,
44+
// Then it should handle these edge cases appropriately, returning 0 for an empty string and counting spaces correctly.
45+
46+
test("should return 0 when the string is empty", () => {
47+
const str = "";
48+
const char = "a";
49+
const count = countChar(str, char);
50+
expect(count).toEqual(0);
51+
});
52+
53+
test("should count characters correctly when the string contains spaces", () => {
54+
const str = "a a a";
55+
const char = "a";
56+
const count = countChar(str, char);
57+
expect(count).toEqual(3);
58+
});
59+
60+
test("should count spaces correctly", () => {
61+
const str = "a a a";
62+
const char = " ";
63+
const count = countChar(str, char);
64+
expect(count).toEqual(2);
65+
});
66+
67+
// Scenario: Uppercase and Lowercase Characters
68+
// Given the input string `str`,
69+
// And a character `char` that is either uppercase or lowercase.
70+
// When the function is called with these inputs,
71+
// Then it should be case-sensitive, counting occurrences of `char` based on its exact case.
72+
73+
test("should be case-sensitive when searching for lowercase letters", () => {
74+
const str = "Apple";
75+
const char = "a";
76+
const count = countChar(str, char);
77+
expect(count).toEqual(0);
78+
});
79+
80+
test("should be case-sensitive when searching for uppercase letters", () => {
81+
const str = "Apple";
82+
const char = "A";
83+
const count = countChar(str, char);
84+
expect(count).toEqual(1);
85+
});
Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
function repeatStr(stringOfCharacters, count) {
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-
5-
if (count === 0) {
6-
return "";
2+
if (count < 0) {
3+
throw new Error("Invalid input: count must be a non-negative integer");
74
}
85

9-
if (count > 0) {
10-
return repeatStr(stringOfCharacters, count - 1) + stringOfCharacters;
6+
let result = "";
7+
8+
for (let i = 0; i < count; i++) {
9+
result += stringOfCharacters;
1110
}
1211

13-
throw new Error("Invalid input: count must be a non-negative integer");
12+
return result;
1413
}
1514

1615
module.exports = repeatStr;

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,19 @@ const repeatStr = require("./repeat-str");
44
// When the repeatStr function is called with these inputs,
55
// Then it should:
66

7+
// Case: Handle negative count:
8+
// Given a target string `str` and a negative integer `count`,
9+
// When the repeatStr function is called with these inputs,
10+
// Then it should throw an error, as negative counts are not valid.
11+
12+
test("should throw an error when count is negative", () => {
13+
const str = "error";
14+
const count = -2;
15+
expect(() => repeatStr(str, count)).toThrow(
16+
"Invalid input: count must be a non-negative integer"
17+
);
18+
});
19+
720
// Case: handle multiple repetitions:
821
// Given a target string `str` and a positive integer `count` greater than 1,
922
// When the repeatStr function is called with these inputs,
@@ -39,16 +52,3 @@ test("should return an empty string when count is 0", () => {
3952
const repeatedStr = repeatStr(str, count);
4053
expect(repeatedStr).toEqual("");
4154
});
42-
43-
// Case: Handle negative count:
44-
// Given a target string `str` and a negative integer `count`,
45-
// When the repeatStr function is called with these inputs,
46-
// Then it should throw an error, as negative counts are not valid.
47-
48-
test("should throw an error when count is negative", () => {
49-
const str = "error";
50-
const count = -2;
51-
expect(() => repeatStr(str, count)).toThrow(
52-
"Invalid input: count must be a non-negative integer"
53-
);
54-
});

0 commit comments

Comments
 (0)