Skip to content

Commit 372e512

Browse files
committed
test: add comprehensive tests for getOrdinalNumber covering all ordinal cases
1 parent 11b7791 commit 372e512

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed
Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
function getOrdinalNumber(num) {
2-
return "1st";
2+
const lastDigit = num % 10;
3+
const lastTwoDigits = num % 100;
4+
5+
if (lastTwoDigits === 11 || lastTwoDigits === 12 || lastTwoDigits === 13) {
6+
return `${num}th`;
7+
}
8+
if (lastDigit === 1) return `${num}st`;
9+
if (lastDigit === 2) return `${num}nd`;
10+
if (lastDigit === 3) return `${num}rd`;
11+
return `${num}th`;
312
}
413

514
module.exports = getOrdinalNumber;

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,59 @@ const getOrdinalNumber = require("./get-ordinal-number");
1111
test("should return '1st' for 1", () => {
1212
expect(getOrdinalNumber(1)).toEqual("1st");
1313
});
14+
15+
test("should return '2nd' for 2", () => {
16+
expect(getOrdinalNumber(2)).toEqual("2nd");
17+
});
18+
19+
test("should return '3rd' for 3", () => {
20+
expect(getOrdinalNumber(3)).toEqual("3rd");
21+
});
22+
23+
test("should handle numbers ending in 1 (except 11)", () => {
24+
expect(getOrdinalNumber(21)).toEqual("21st");
25+
expect(getOrdinalNumber(31)).toEqual("31st");
26+
expect(getOrdinalNumber(101)).toEqual("101st");
27+
});
28+
29+
test("should handle numbers ending in 2 (except 12)", () => {
30+
expect(getOrdinalNumber(22)).toEqual("22nd");
31+
expect(getOrdinalNumber(42)).toEqual("42nd");
32+
});
33+
34+
test("should handle numbers ending in 3 (except 13)", () => {
35+
expect(getOrdinalNumber(23)).toEqual("23rd");
36+
expect(getOrdinalNumber(33)).toEqual("33rd");
37+
});
38+
39+
test("should handle special cases 11, 12, 13", () => {
40+
expect(getOrdinalNumber(11)).toEqual("11th");
41+
expect(getOrdinalNumber(12)).toEqual("12th");
42+
expect(getOrdinalNumber(13)).toEqual("13th");
43+
});
44+
45+
test("should handle numbers ending in 4-9, 0", () => {
46+
expect(getOrdinalNumber(4)).toEqual("4th");
47+
expect(getOrdinalNumber(5)).toEqual("5th");
48+
expect(getOrdinalNumber(10)).toEqual("10th");
49+
});
50+
51+
test("should return '100th' for 100", () => {
52+
expect(getOrdinalNumber(100)).toEqual("100th");
53+
});
54+
55+
test("should return '24th' for 24", () => {
56+
expect(getOrdinalNumber(24)).toEqual("24th");
57+
});
58+
59+
test("should return '111th' for 111", () => {
60+
expect(getOrdinalNumber(111)).toEqual("111th");
61+
});
62+
63+
test("should return '112th' for 112", () => {
64+
expect(getOrdinalNumber(112)).toEqual("112th");
65+
});
66+
67+
test("should return '113th' for 113", () => {
68+
expect(getOrdinalNumber(113)).toEqual("113th");
69+
});

0 commit comments

Comments
 (0)