-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget-ordinal-number.js
More file actions
39 lines (36 loc) · 781 Bytes
/
get-ordinal-number.js
File metadata and controls
39 lines (36 loc) · 781 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
function getOrdinalNumber(num) {
if (arguments.length !== 1) {
throw new Error("Function requires exactly one argument");
}
if (typeof num !== "number") {
throw new Error("Input must be a number");
}
if (!isFinite(num)) {
throw new Error("Input must be a finite number");
}
if (!Number.isInteger(num) || num < 0) {
throw new Error("Input must be a non-negative integer");
}
switch (num % 100) {
case 11:
case 12:
case 13:
return num + "th";
break;
}
switch (num % 10) {
case 1:
return num + "st";
break;
case 2:
return num + "nd";
break;
case 3:
return num + "rd";
break;
default:
return num + "th";
break;
}
}
module.exports = getOrdinalNumber;