Skip to content

Commit 5394a9e

Browse files
committed
Address the issues from the pull request comments
1 parent 4002e46 commit 5394a9e

File tree

4 files changed

+68
-5
lines changed

4 files changed

+68
-5
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,22 @@
11
function countChar(stringOfCharacters, findCharacter) {
2+
if (typeof stringOfCharacters !== "string") {
3+
throw new TypeError(
4+
"The argument for parameter stringOfCharacters is not a String type"
5+
);
6+
}
7+
8+
if (typeof findCharacter !== "string") {
9+
throw new TypeError(
10+
"The argument for parameter findCharacter is not a String type"
11+
);
12+
}
13+
14+
if (findCharacter.length !== 1) {
15+
throw new Error(
16+
"Parameter findCharacter is must contain a single character"
17+
);
18+
}
19+
220
return stringOfCharacters.split("").reduce((accumulator, character) => {
321
return accumulator + (character === findCharacter ? 1 : 0);
422
}, 0);

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

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,48 @@ test("should count multiple occurrences of a character", () => {
2323
// When the function is called with these inputs,
2424
// Then it should return 0, indicating that no occurrences of the char were found in the case-sensitive str.
2525

26-
test("should count no occurences of z characters", () => {
26+
test("should count no occurrences of z characters", () => {
2727
const str = "aaaaa";
2828
const char = "z";
2929
const count = countChar(str, char);
3030
expect(count).toEqual(0);
3131
});
32+
33+
test("should return 0 for an empty string and a non-empty character", () => {
34+
const str = "";
35+
const char = "z";
36+
const count = countChar(str, char);
37+
expect(count).toEqual(0);
38+
});
39+
40+
test("should return 0 for an empty string and a whitespace character", () => {
41+
const str = "";
42+
const char = " ";
43+
const count = countChar(str, char);
44+
expect(count).toEqual(0);
45+
});
46+
47+
test("should return 0 when the string contains the character in a different case", () => {
48+
const str = "aBcDeF";
49+
const char = "A";
50+
const count = countChar(str, char);
51+
expect(count).toEqual(0);
52+
});
53+
54+
test("throws error when the first argument is not a string", () => {
55+
expect(() => countChar(1234, "1")).toThrow(
56+
"The argument for parameter stringOfCharacters is not a String type"
57+
);
58+
});
59+
60+
test("throws error when the second argument is not a string", () => {
61+
expect(() => countChar("1234", 1)).toThrow(
62+
"The argument for parameter findCharacter is not a String type"
63+
);
64+
});
65+
66+
test("throws error when second argument is longer than one character", () => {
67+
expect(() => countChar("1234", "12")).toThrow(
68+
"Parameter findCharacter is must contain a single character"
69+
);
70+
});

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@ function getOrdinalNumber(num) {
88
const lastDigits = Number(numberString.slice(-2));
99
let ordinal;
1010

11-
console.log("lastDigits", lastDigits);
12-
1311
if (num === 0) {
1412
ordinal = "";
15-
} else if (lastDigits === 11) {
13+
} else if ([11, 12, 13].includes(lastDigits)) {
1614
ordinal = "th";
1715
} else if (lastDigit === 1) {
1816
ordinal = "st";

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const getOrdinalNumber = require("./get-ordinal-number");
88
// When the number is 1,
99
// Then the function should return "1st"
1010

11-
test("should return '@' for ", () => {
11+
test("should return an empty string for a non-numerical character", () => {
1212
expect(getOrdinalNumber("@")).toEqual("");
1313
});
1414

@@ -20,6 +20,14 @@ test("should return '11th' for 11", () => {
2020
expect(getOrdinalNumber(11)).toEqual("11th");
2121
});
2222

23+
test("should return '12th' for 12", () => {
24+
expect(getOrdinalNumber(12)).toEqual("12th");
25+
});
26+
27+
test("should return '13th' for 13", () => {
28+
expect(getOrdinalNumber(13)).toEqual("13th");
29+
});
30+
2331
test("should return '-11th' for -11", () => {
2432
expect(getOrdinalNumber(-11)).toEqual("-11th");
2533
});

0 commit comments

Comments
 (0)