Skip to content

Commit fdea2dd

Browse files
committed
Ajustments made on Sprint-3/2-practice-tdd to make the tests pass. The functions were implemented in a way that they return the expected output for the given test cases.
1 parent 3372770 commit fdea2dd

File tree

3 files changed

+95
-11
lines changed

3 files changed

+95
-11
lines changed

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

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,44 @@ const countChar = require("./count");
1111
// Then it should correctly count occurrences of `char`.
1212

1313
test("should count multiple occurrences of a character", () => {
14-
const str = "aaaaa";
15-
const char = "a";
16-
const count = countChar(str, char);
17-
expect(count).toEqual(5);
14+
const str = "aaaaa";
15+
const char = "a";
16+
const count = countChar(str, char);
17+
expect(count).toEqual(5);
1818
});
1919

2020
// Scenario: No Occurrences
2121
// Given the input string `str`,
2222
// And a character `char` that does not exist within `str`.
2323
// When the function is called with these inputs,
2424
// Then it should return 0, indicating that no occurrences of `char` were found.
25+
test("should return 0 when character does not occur in the string", () => {
26+
const str = "hello world";
27+
const char = "x";
28+
const count = countChar(str, char);
29+
expect(count).toEqual(0);
30+
});
31+
32+
// Scenario: Empty String
33+
// Given an empty string `str`,
34+
// And any character `char` (e.g., 'a'),
35+
// When the function is called with these inputs,
36+
// Then it should return 0, as there are no characters to count in an empty string.
37+
test("should return 0 when the input string is empty", () => {
38+
const str = "";
39+
const char = "a";
40+
const count = countChar(str, char);
41+
expect(count).toEqual(0);
42+
});
43+
44+
// Scenario: Case Sensitivity
45+
// Given the input string `str`,
46+
// And a character `char` that exists in `str` but with different cases (e.g., 'a' and 'A'),
47+
// When the function is called with these inputs,
48+
// Then it should count occurrences of `char` in a case-sensitive manner, treating uppercase and lowercase characters as distinct.
49+
test("should count characters in a case-sensitive manner", () => {
50+
const str = "AaAaA";
51+
const char = "a";
52+
const count = countChar(str, char);
53+
expect(count).toEqual(3);
54+
});

Sprint-3/2-practice-tdd/get-ordinal-number.test.js

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,44 @@ const getOrdinalNumber = require("./get-ordinal-number");
1414
// When the number ends with 1, except those ending with 11,
1515
// Then the function should return a string by appending "st" to the number.
1616
test("should append 'st' for numbers ending with 1, except those ending with 11", () => {
17-
expect(getOrdinalNumber(1)).toEqual("1st");
18-
expect(getOrdinalNumber(21)).toEqual("21st");
19-
expect(getOrdinalNumber(131)).toEqual("131st");
17+
expect(getOrdinalNumber(1)).toEqual("1st");
18+
expect(getOrdinalNumber(21)).toEqual("21st");
19+
expect(getOrdinalNumber(131)).toEqual("131st");
2020
});
21+
22+
// Case 2: Numbers ending with 2 (but not 12)
23+
// When the number ends with 2, except those ending with 12,
24+
// Then the function should return a string by appending "nd" to the number.
25+
test("should append 'nd' for numbers ending with 2, except those ending with 12", () => {
26+
expect(getOrdinalNumber(2)).toEqual("2nd");
27+
expect(getOrdinalNumber(22)).toEqual("22nd");
28+
expect(getOrdinalNumber(132)).toEqual("132nd");
29+
});
30+
31+
// Case 3: Numbers ending with 3 (but not 13)
32+
// When the number ends with 3, except those ending with 13,
33+
// Then the function should return a string by appending "rd" to the number.
34+
test("should append 'rd' for numbers ending with 3, except those ending with 13", () => {
35+
expect(getOrdinalNumber(3)).toEqual("3rd");
36+
expect(getOrdinalNumber(23)).toEqual("23rd");
37+
expect(getOrdinalNumber(133)).toEqual("133rd");
38+
});
39+
40+
// Case 4: Numbers ending with 11, 12, or 13
41+
// When the number ends with 11, 12, or 13,
42+
// Then the function should return a string by appending "th" to the number.
43+
test("should append 'th' for numbers ending with 11, 12, or 13", () => {
44+
expect(getOrdinalNumber(11)).toEqual("11th");
45+
expect(getOrdinalNumber(12)).toEqual("12th");
46+
expect(getOrdinalNumber(13)).toEqual("13th");
47+
});
48+
49+
// Case 5: All other numbers
50+
// When the number does not end with 1, 2, or 3 (except those ending with 11, 12, or 13),
51+
// Then the function should return a string by appending "th" to the number.
52+
test("should append 'th' for all other numbers", () => {
53+
expect(getOrdinalNumber(4)).toEqual("4th");
54+
expect(getOrdinalNumber(10)).toEqual("10th");
55+
expect(getOrdinalNumber(14)).toEqual("14th");
56+
expect(getOrdinalNumber(100)).toEqual("100th");
57+
});

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

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,40 @@ const repeatStr = require("./repeat-str");
1010
// Then it should return a string that contains the original `str` repeated `count` times.
1111

1212
test("should repeat the string count times", () => {
13-
const str = "hello";
14-
const count = 3;
15-
const repeatedStr = repeatStr(str, count);
16-
expect(repeatedStr).toEqual("hellohellohello");
13+
const str = "hello";
14+
const count = 3;
15+
const repeatedStr = repeatStr(str, count);
16+
expect(repeatedStr).toEqual("hellohellohello");
1717
});
1818

1919
// Case: handle count of 1:
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 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+
});
2329

2430
// Case: Handle count of 0:
2531
// Given a target string `str` and a `count` equal to 0,
2632
// When the repeatStr function is called with these inputs,
2733
// Then it should return an empty string.
34+
test("should return an empty string when count is 0", () => {
35+
const str = "test";
36+
const count = 0;
37+
const repeatedStr = repeatStr(str, count);
38+
expect(repeatedStr).toEqual("");
39+
});
2840

2941
// Case: Handle negative count:
3042
// Given a target string `str` and a negative integer `count`,
3143
// When the repeatStr function is called with these inputs,
3244
// Then it should throw an error, as negative counts are not valid.
45+
test("should throw an error when count is negative", () => {
46+
const str = "error";
47+
const count = -1;
48+
expect(() => repeatStr(str, count)).toThrow("Count must be a non-negative integer");
49+
});

0 commit comments

Comments
 (0)