File tree Expand file tree Collapse file tree 1 file changed +27
-8
lines changed
Expand file tree Collapse file tree 1 file changed +27
-8
lines changed Original file line number Diff line number Diff line change 11function getOrdinalNumber ( num ) {
2- if ( num === 1 || num === 21 ) {
3- return `${ num } st` ;
4- } else if ( num === 11 ) {
5- return `${ num } th` ;
6- } else if ( num === 2 ) {
7- return `${ num } nd` ;
2+ let ordinalNum ;
3+ if ( Number . isInteger ( num ) === true ) {
4+ if (
5+ Math . abs ( num % 100 ) === 11 ||
6+ Math . abs ( num % 100 ) === 12 ||
7+ Math . abs ( num % 100 ) === 13 ||
8+ Math . abs ( num % 10 ) === 4 ||
9+ Math . abs ( num % 10 ) === 5 ||
10+ Math . abs ( num % 10 ) === 6 ||
11+ Math . abs ( num % 10 ) === 7 ||
12+ Math . abs ( num % 10 ) === 8 ||
13+ Math . abs ( num % 10 ) === 9 ||
14+ Math . abs ( num % 10 ) === 0
15+ ) {
16+ ordinalNum = `${ num } th` ;
17+ } else if ( Math . abs ( num % 10 ) === 1 ) {
18+ ordinalNum = `${ num } st` ;
19+ } else if ( Math . abs ( num % 10 ) === 2 ) {
20+ ordinalNum = `${ num } nd` ;
21+ } else if ( Math . abs ( num % 10 ) === 3 ) {
22+ ordinalNum = `${ num } rd` ;
23+ } else {
24+ ordinalNum = `${ num } th` ;
25+ }
26+ } else {
27+ ordinalNum = "Invalid input: Input is an integer" ;
828 }
29+ return ordinalNum ;
930}
10-
11-
1231module . exports = getOrdinalNumber ;
You can’t perform that action at this time.
0 commit comments