Skip to content

Commit 2f7741e

Browse files
committed
add implementation and correctly handle st, nd, rd, th suffixes including 11, 12, 13
1 parent 3d45811 commit 2f7741e

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed
Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
function getOrdinalNumber(num) {
2-
return "1st";
2+
const lastTwo = num % 100; // handle teen exceptions
3+
if (lastTwo === 11 || lastTwo === 12 || lastTwo === 13) {
4+
return num + "th";
5+
}
6+
7+
const lastDigit = num % 10;
8+
if (lastDigit === 1) return num + "st";
9+
if (lastDigit === 2) return num + "nd";
10+
if (lastDigit === 3) return num + "rd";
11+
12+
return num + "th";
313
}
414

515
module.exports = getOrdinalNumber;

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,14 @@ test("should append 'th' for numbers ending with 11, 12, or 13", () => {
4747
});
4848

4949
// Case 4: All other numbers
50-
test("should append 'th' for numbers that do not follow the 1, 2, 3 rule (e.g., 4, 5, 6...)", () => {
50+
51+
test("should append 'th' for numbers that do not follow the 1, 2, 3 rule", () => {
52+
// Single digits 4-10
5153
expect(getOrdinalNumber(4)).toEqual("4th");
5254
expect(getOrdinalNumber(5)).toEqual("5th");
53-
});
55+
expect(getOrdinalNumber(6)).toEqual("6th");
56+
expect(getOrdinalNumber(7)).toEqual("7th");
57+
expect(getOrdinalNumber(8)).toEqual("8th");
58+
expect(getOrdinalNumber(9)).toEqual("9th");
59+
expect(getOrdinalNumber(10)).toEqual("10th");
60+
});

0 commit comments

Comments
 (0)