-
-
Notifications
You must be signed in to change notification settings - Fork 337
Expand file tree
/
Copy pathget-ordinal-number.js
More file actions
39 lines (33 loc) · 770 Bytes
/
get-ordinal-number.js
File metadata and controls
39 lines (33 loc) · 770 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
/**
* Original file
*
function getOrdinalNumber(num) {
return "1st";
}
module.exports = getOrdinalNumber;
*
* Enf of file
*/
function getOrdinalNumber(number) {
if (typeof number !== 'number' || !Number.isInteger(number) || number < 0) {
throw new Error('Input must be a non-negative integer');
}
// Special case for numbers ending in 11, 12, 13
const lastTwoDigits = number % 100;
if (lastTwoDigits >= 11 && lastTwoDigits <= 13) {
return number + "th";
}
// Check the last digit
const lastDigit = number % 10;
switch (lastDigit) {
case 1:
return number + "st";
case 2:
return number + "nd";
case 3:
return number + "rd";
default:
return number + "th";
}
}
module.exports = getOrdinalNumber;