Skip to content

Commit a1a791e

Browse files
committed
Function implemented
1 parent 826a780 commit a1a791e

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed
Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,25 @@
11
function getOrdinalNumber(num) {
2-
return "1st";
2+
if (!Number.isInteger(num) || num <= 0) {
3+
throw new Error("Only positive integers are allowed");
4+
}
5+
// Handle Exception of 11, 12, 13
6+
if (num % 100 >= 11 && num % 100 <= 13) {
7+
return num + "th";
8+
}
9+
// Handle general last-digit cases
10+
switch (num % 10) {
11+
case 1:
12+
return num + "st";
13+
case 2:
14+
return num + "nd";
15+
case 3:
16+
return num + "rd";
17+
default:
18+
return num + "th";
19+
}
320
}
421

22+
523
module.exports = getOrdinalNumber;
24+
25+
// Function implemented

0 commit comments

Comments
 (0)