Skip to content

Commit 26e6bc9

Browse files
committed
TDD getOrdinalNumber: handle nd, rd, th, and teen exceptions
1 parent de8c4b0 commit 26e6bc9

2 files changed

Lines changed: 62 additions & 1 deletion

File tree

‎practice-tdd/get-ordinal-number.js‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
11
export function getOrdinalNumber(num) {
2-
return "1st";
2+
const lastTwoDigits = num % 100;
3+
4+
if (lastTwoDigits >= 11 && lastTwoDigits <= 13) {
5+
return `${num}th`;
6+
}
7+
8+
const lastDigit = num % 10;
9+
10+
if (lastDigit === 1) {
11+
return `${num}st`;
12+
}
13+
14+
if (lastDigit === 2) {
15+
return `${num}nd`;
16+
}
17+
18+
if (lastDigit === 3) {
19+
return `${num}rd`;
20+
}
21+
22+
return `${num}th`;
323
}

‎practice-tdd/get-ordinal-number.test.js‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,44 @@ test("should append 'st' for numbers ending with 1, except those ending with 11"
1818
expect(getOrdinalNumber(21)).toEqual("21st");
1919
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+
// regardless of the rules for 1, 2, or 3.
44+
test("should append 'th' for numbers ending with 11, 12, or 13", () => {
45+
expect(getOrdinalNumber(11)).toEqual("11th");
46+
expect(getOrdinalNumber(12)).toEqual("12th");
47+
expect(getOrdinalNumber(13)).toEqual("13th");
48+
expect(getOrdinalNumber(111)).toEqual("111th");
49+
expect(getOrdinalNumber(112)).toEqual("112th");
50+
expect(getOrdinalNumber(113)).toEqual("113th");
51+
});
52+
53+
// Case 5: Numbers ending with 0 or 4-9
54+
// When the number ends with any digit other than 1, 2, or 3 (and is not 11, 12, or 13),
55+
// Then the function should return a string by appending "th" to the number.
56+
test("should append 'th' for numbers ending with 0 or 4-9", () => {
57+
expect(getOrdinalNumber(4)).toEqual("4th");
58+
expect(getOrdinalNumber(10)).toEqual("10th");
59+
expect(getOrdinalNumber(100)).toEqual("100th");
60+
expect(getOrdinalNumber(109)).toEqual("109th");
61+
});

0 commit comments

Comments
 (0)