Skip to content

Commit fd6e675

Browse files
committed
Added getOrdinalNumber function
1 parent b8e36c4 commit fd6e675

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed
Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
1-
function getOrdinalNumber(num) {
2-
return "1st";
1+
function getOrdinalNumber(num){
2+
const lastTwoDigits = num % 100;
3+
if (lastTwoDigits >= 11 && lastTwoDigits <= 13){
4+
return num+ "th";
5+
}
6+
7+
const lastDigits = num %10;
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)