Skip to content

Commit 7c2f507

Browse files
implement getOrdinalNumber function to return correct ordinal suffixes for numbers
1 parent ffc428f commit 7c2f507

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+
const absNum = Math.abs(num);
3+
const lastTwoDigits = absNum % 100;
4+
5+
if (lastTwoDigits >= 11 && lastTwoDigits <= 13) {
6+
return `${num}th`;
7+
}
8+
9+
const lastDigit = absNum % 10;
10+
11+
if (lastDigit === 1) {
12+
return `${num}st`;
13+
}
14+
15+
if (lastDigit === 2) {
16+
return `${num}nd`;
17+
}
18+
19+
if (lastDigit === 3) {
20+
return `${num}rd`;
21+
}
22+
23+
return `${num}th`;
324
}
425

526
module.exports = getOrdinalNumber;

0 commit comments

Comments
 (0)