Skip to content

Commit 54db0e5

Browse files
committed
Implement getOrdinalNumber and add full test coverage
1 parent 47d8723 commit 54db0e5

2 files changed

Lines changed: 32 additions & 13 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ function getOrdinalNumber(num) {
88
return `${num}th`;
99
}
1010

11-
module.exports = getOrdinalNumber;
11+
module.exports = getOrdinalNumber;
Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,39 @@
11
const getOrdinalNumber = require("./get-ordinal-number");
2-
// In this week's prep, we started implementing getOrdinalNumber.
3-
4-
// Continue testing and implementing getOrdinalNumber for additional cases.
5-
// Write your tests using Jest — remember to run your tests often for continual feedback.
6-
7-
// To ensure thorough testing, we need broad scenarios that cover all possible cases.
8-
// Listing individual values, however, can quickly lead to an unmanageable number of test cases.
9-
// Instead of writing tests for individual numbers, consider grouping all possible input values
10-
// into meaningful categories. Then, select representative samples from each category to test.
11-
// This approach improves coverage and makes our tests easier to maintain.
122

133
// Case 1: Numbers ending with 1 (but not 11)
14-
// When the number ends with 1, except those ending with 11,
15-
// Then the function should return a string by appending "st" to the number.
164
test("should append 'st' for numbers ending with 1, except those ending with 11", () => {
175
expect(getOrdinalNumber(1)).toEqual("1st");
186
expect(getOrdinalNumber(21)).toEqual("21st");
197
expect(getOrdinalNumber(131)).toEqual("131st");
208
});
9+
10+
// Case 2: Numbers ending with 2 (but not 12)
11+
test("should append 'nd' for numbers ending with 2, except those ending with 12", () => {
12+
expect(getOrdinalNumber(2)).toEqual("2nd");
13+
expect(getOrdinalNumber(22)).toEqual("22nd");
14+
expect(getOrdinalNumber(132)).toEqual("132nd");
15+
});
16+
17+
// Case 3: Numbers ending with 3 (but not 13)
18+
test("should append 'rd' for numbers ending with 3, except those ending with 13", () => {
19+
expect(getOrdinalNumber(3)).toEqual("3rd");
20+
expect(getOrdinalNumber(33)).toEqual("33rd");
21+
expect(getOrdinalNumber(133)).toEqual("133rd");
22+
});
23+
24+
// Case 4: Numbers ending with 11, 12, or 13 are special and take "th"
25+
test("should append 'th' for numbers ending with 11, 12, or 13", () => {
26+
expect(getOrdinalNumber(11)).toEqual("11th");
27+
expect(getOrdinalNumber(12)).toEqual("12th");
28+
expect(getOrdinalNumber(13)).toEqual("13th");
29+
expect(getOrdinalNumber(111)).toEqual("111th");
30+
expect(getOrdinalNumber(112)).toEqual("112th");
31+
expect(getOrdinalNumber(113)).toEqual("113th");
32+
});
33+
34+
// Case 5: All other numbers
35+
test("should append 'th' for all other numbers", () => {
36+
expect(getOrdinalNumber(4)).toEqual("4th");
37+
expect(getOrdinalNumber(10)).toEqual("10th");
38+
expect(getOrdinalNumber(100)).toEqual("100th");
39+
});

0 commit comments

Comments
 (0)