Skip to content

Commit 42be3ff

Browse files
committed
Implement getOrdinalNumber function with edge case handling
1 parent 3248c54 commit 42be3ff

File tree

1 file changed

+15
-1
lines changed

1 file changed

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

519
module.exports = getOrdinalNumber;

0 commit comments

Comments
 (0)