Skip to content

Commit 0b8375e

Browse files
committed
Completed getOrdinalNumber implementation - passes all 5 Jest test cases
1 parent a4eb4d3 commit 0b8375e

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed
Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
11
function getOrdinalNumber(num) {
2-
return "1st";
2+
// Last digit determines st/nd/rd for most numbers
3+
const lastDigit = num % 10;
4+
// Last two digits needed to handle special,
5+
// teen cases (11th, 12th, 13th)
6+
const lastTwo = num % 100;
7+
8+
// Special rule: numbers ending in 11, 12 or 13 always use "th"
9+
if (lastTwo === 11 || lastTwo === 12 || lastTwo === 13) {
10+
return num + "th";
11+
}
12+
13+
// Ending in 1 → "st"
14+
if (lastDigit === 1) return num + "st";
15+
// Ending in 2 → "nd"
16+
if (lastDigit === 2) return num + "nd";
17+
// Ending in 3 → "rd"
18+
if (lastDigit === 3) return num + "rd";
19+
// All other endings (0, 4-9 and anything after teen check) → "th"
20+
return num + "th";
321
}
422

523
module.exports = getOrdinalNumber;

0 commit comments

Comments
 (0)