Skip to content

Commit a2c64ac

Browse files
committed
Implement getOrdinalNumber using TDD with grouped test categories
1 parent c870aac commit a2c64ac

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed
Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
function getOrdinalNumber(num) {
2-
return "1st";
2+
const lastDigit = num % 10;
3+
const lastTwoDigits = num % 100;
4+
5+
if (lastDigit === 1 && lastTwoDigits !== 11) {
6+
return num + "st";
7+
} else if (lastDigit === 2 && lastTwoDigits !== 12) {
8+
return num + "nd";
9+
} else if (lastDigit === 3 && lastTwoDigits !== 13) {
10+
return num + "rd";
11+
} else {
12+
return num + "th";
13+
}
314
}
415

516
module.exports = getOrdinalNumber;

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,32 @@ test("should append 'st' for numbers ending with 1, except those ending with 11"
1717
expect(getOrdinalNumber(1)).toEqual("1st");
1818
expect(getOrdinalNumber(21)).toEqual("21st");
1919
expect(getOrdinalNumber(131)).toEqual("131st");
20+
expect(getOrdinalNumber(31)).toEqual("31st");
21+
expect(getOrdinalNumber(11)).toEqual("11th");
2022
});
23+
24+
// Case 2: Numbers ending with 2 but not 12)
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+
expect(getOrdinalNumber(12)).toEqual("12th"); // exception
30+
});
31+
32+
// Case 3: Numbers ending with 3 (but not 13
33+
test("should append 'rd' for numbers ending with 3, except those ending with 13", () => {
34+
expect(getOrdinalNumber(3)).toEqual("3rd");
35+
expect(getOrdinalNumber(23)).toEqual("23rd");
36+
expect(getOrdinalNumber(143)).toEqual("143rd");
37+
expect(getOrdinalNumber(13)).toEqual("13th"); // exception
38+
});
39+
40+
// Case 4: All other numbers
41+
test("should append 'th' for all other numbers", () => {
42+
expect(getOrdinalNumber(4)).toEqual("4th");
43+
expect(getOrdinalNumber(8)).toEqual("8th");
44+
expect(getOrdinalNumber(10)).toEqual("10th");
45+
expect(getOrdinalNumber(35)).toEqual("35th");
46+
expect(getOrdinalNumber(100)).toEqual("100th");
47+
expect(getOrdinalNumber(111)).toEqual("111th"); // ends with 11 special case
48+
});

0 commit comments

Comments
 (0)