Skip to content

Commit ac7e1c8

Browse files
Implement getOrdinalNumber function to return correct ordinal suffixes
1 parent 0141efb commit ac7e1c8

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed
Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,26 @@
11
function getOrdinalNumber(num) {
2-
return "1st";
2+
// Calculate the remainder when divided by 100 to check for 11, 12, 13
3+
const lastTwoDigits = number % 100;
4+
5+
// Calculate the remainder when divided by 10 to get the single last digit
6+
const lastDigit = number % 10;
7+
8+
// 1. The "teen" exceptions (11th, 12th, 13th)
9+
if (lastTwoDigits >= 11 && lastTwoDigits <= 13) {
10+
return number + "th";
11+
}
12+
13+
// 2. Apply standard rules based on last digit
14+
if (lastDigit === 1) {
15+
return number + "st";
16+
} else if (lastDigit === 2) {
17+
return number + "nd";
18+
} else if (lastDigit === 3) {
19+
return number + "rd";
20+
} else {
21+
// Cover 0, 4, 5, 6, 7, 8, and 9
22+
return number + "th";
23+
}
324
}
425

526
module.exports = getOrdinalNumber;

0 commit comments

Comments
 (0)