Skip to content

Commit 745b6e9

Browse files
committed
function updated
1 parent 620ee39 commit 745b6e9

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed
Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
function getOrdinalNumber(num) {
2-
return "1st";
2+
const lastTwoDigits = num % 100; // check the last 2 digits for 11,12,13
3+
const lastDigit = num % 10;
4+
5+
if (lastTwoDigits >= 11 && lastTwoDigits <= 13) {
6+
return num + "th";
7+
}
8+
9+
switch (lastDigit) {
10+
case 1:
11+
return num + "st";
12+
case 2:
13+
return num + "nd";
14+
case 3:
15+
return num + "rd";
16+
default:
17+
return num + "th";
18+
}
319
}
420

521
module.exports = getOrdinalNumber;

0 commit comments

Comments
 (0)