We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent a4eb4d3 commit 0b8375eCopy full SHA for 0b8375e
1 file changed
Sprint-3/2-practice-tdd/get-ordinal-number.js
@@ -1,5 +1,23 @@
1
function getOrdinalNumber(num) {
2
- return "1st";
+ // Last digit determines st/nd/rd for most numbers
3
+ const lastDigit = num % 10;
4
+ // Last two digits needed to handle special,
5
+ // teen cases (11th, 12th, 13th)
6
+ const lastTwo = num % 100;
7
+
8
+ // Special rule: numbers ending in 11, 12 or 13 always use "th"
9
+ if (lastTwo === 11 || lastTwo === 12 || lastTwo === 13) {
10
+ return num + "th";
11
+ }
12
13
+ // Ending in 1 → "st"
14
+ if (lastDigit === 1) return num + "st";
15
+ // Ending in 2 → "nd"
16
+ if (lastDigit === 2) return num + "nd";
17
+ // Ending in 3 → "rd"
18
+ if (lastDigit === 3) return num + "rd";
19
+ // All other endings (0, 4-9 and anything after teen check) → "th"
20
21
}
22
23
module.exports = getOrdinalNumber;
0 commit comments