File tree Expand file tree Collapse file tree 1 file changed +19
-1
lines changed
Expand file tree Collapse file tree 1 file changed +19
-1
lines changed Original file line number Diff line number Diff line change 11function getOrdinalNumber ( num ) {
2- return "1st" ;
2+ // 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+ return num + "th" ;
321}
422
523module . exports = getOrdinalNumber ;
You can’t perform that action at this time.
0 commit comments