Skip to content

Commit 0280acb

Browse files
committed
Refactor getOrdinalNumber function and enhance test coverage for ordinal suffixes
1 parent 9d13824 commit 0280acb

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed
Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
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+
9+
if (lastDigit === 1) {
10+
return num + "st";
11+
} else if (lastDigit === 2) {
12+
return num + "nd";
13+
} else if (lastDigit === 3) {
14+
return num + "rd";
15+
} else {
16+
return num + "th";
17+
}
318
}
419

520
module.exports = getOrdinalNumber;

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

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,35 @@ const getOrdinalNumber = require("./get-ordinal-number");
1313
// Case 1: Numbers ending with 1 (but not 11)
1414
// When the number ends with 1, except those ending with 11,
1515
// Then the function should return a string by appending "st" to the number.
16-
test("should append 'st' for numbers ending with 1, except those ending with 11", () => {
16+
test("should append 'st' for numbers ending with 1, except 11", () => {
1717
expect(getOrdinalNumber(1)).toEqual("1st");
1818
expect(getOrdinalNumber(21)).toEqual("21st");
19-
expect(getOrdinalNumber(131)).toEqual("131st");
19+
expect(getOrdinalNumber(31)).toEqual("31st");
20+
expect(getOrdinalNumber(101)).toEqual("101st");
2021
});
22+
23+
test("should append 'nd' for numbers ending with 2, except 12", () => {
24+
expect(getOrdinalNumber(2)).toEqual("2nd");
25+
expect(getOrdinalNumber(22)).toEqual("22nd");
26+
expect(getOrdinalNumber(82)).toEqual("82nd");
27+
});
28+
29+
test("should append 'rd' for numbers ending with 3, except 13", () => {
30+
expect(getOrdinalNumber(3)).toEqual("3rd");
31+
expect(getOrdinalNumber(33)).toEqual("33rd");
32+
expect(getOrdinalNumber(93)).toEqual("93rd");
33+
});
34+
35+
test("should append 'th' for the exceptions 11, 12, and 13", () => {
36+
expect(getOrdinalNumber(11)).toEqual("11th");
37+
expect(getOrdinalNumber(12)).toEqual("12th");
38+
expect(getOrdinalNumber(13)).toEqual("13th");
39+
expect(getOrdinalNumber(111)).toEqual("111th");
40+
});
41+
42+
test("should append 'th' for all other numbers", () => {
43+
expect(getOrdinalNumber(4)).toEqual("4th");
44+
expect(getOrdinalNumber(10)).toEqual("10th");
45+
expect(getOrdinalNumber(35)).toEqual("35th");
46+
expect(getOrdinalNumber(99)).toEqual("99th");
47+
expect(getOrdinalNumber(100)).toEqual("100th");

0 commit comments

Comments
 (0)