File tree Expand file tree Collapse file tree 1 file changed +15
-13
lines changed
Expand file tree Collapse file tree 1 file changed +15
-13
lines changed Original file line number Diff line number Diff line change 11function getOrdinalNumber ( num ) {
2- if ( num === 0 ) {
2+ if ( num === 0 ) {
33 return "0" ;
44 }
5+
56 const lastTwoDigits = num % 100 ;
67 const lastDigit = num % 10 ;
78
8- if ( lastTwoDigits === 11 || lastTwoDigits === 12 || lastTwoDigits === 13 ) {
9- return `${ num } th` ;
10- }
11- if ( lastDigit === 1 ) {
12- return `${ num } st` ;
13- }
14- if ( lastDigit === 2 ) {
15- return `${ num } nd` ;
16- }
17- if ( lastDigit === 3 ) {
18- return `${ num } rd` ;
9+ switch ( true ) {
10+ case lastTwoDigits === 11 :
11+ case lastTwoDigits === 12 :
12+ case lastTwoDigits === 13 :
13+ return `${ num } th` ;
14+ case lastDigit === 1 :
15+ return `${ num } st` ;
16+ case lastDigit === 2 :
17+ return `${ num } nd` ;
18+ case lastDigit === 3 :
19+ return `${ num } rd` ;
20+ default :
21+ return `${ num } th` ;
1922 }
20- return `${ num } th` ;
2123}
2224
2325module . exports = getOrdinalNumber ;
You can’t perform that action at this time.
0 commit comments