Skip to content

Commit 08136b1

Browse files
committed
added comments to the function getOrdinalNumber
1 parent ad68622 commit 08136b1

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// get-ordinal-number.js
2+
function getOrdinalNumber(num) {
3+
// Check if the input is a number and an integer
4+
if (typeof num !== "number" || !Number.isInteger(num)) {
5+
throw new Error("Input must be an integer."); // Throw an error if input is not an integer
6+
}
7+
8+
// Array of suffixes for ordinal numbers
9+
const suffixes = ["th", "st", "nd", "rd"];
10+
11+
// Get the last two digits of the number
12+
const v = num % 100;
13+
14+
// Special case handling for 11th, 12th, and 13th
15+
if (v >= 11 && v <= 13) {
16+
return num + "th"; // Always use "th" for these cases
17+
}
18+
19+
// Use the suffix based on the last digit
20+
return num + (suffixes[num % 10] || "th"); // Default to "th" if no specific suffix is found
21+
}
22+
23+
module.exports = getOrdinalNumber; // Export the function
24+

0 commit comments

Comments
 (0)