File tree Expand file tree Collapse file tree 2 files changed +45
-1
lines changed
Expand file tree Collapse file tree 2 files changed +45
-1
lines changed Original file line number Diff line number Diff line change 11function getOrdinalNumber ( num ) {
2- return "1st" ;
2+ const remainderTen = num % 10 ;
3+ const remainderHundred = num % 100 ;
4+
5+ if ( remainderHundred >= 11 && remainderHundred <= 13 ) {
6+ return `${ num } th` ;
7+ }
8+
9+ switch ( remainderTen ) {
10+ case 1 :
11+ return `${ num } st` ;
12+ case 2 :
13+ return `${ num } nd` ;
14+ case 3 :
15+ return `${ num } rd` ;
16+ default :
17+ return `${ num } th` ;
18+ }
319}
420
521module . exports = getOrdinalNumber ;
Original file line number Diff line number Diff line change @@ -11,3 +11,31 @@ const getOrdinalNumber = require("./get-ordinal-number");
1111test ( "should return '1st' for 1" , ( ) => {
1212 expect ( getOrdinalNumber ( 1 ) ) . toEqual ( "1st" ) ;
1313} ) ;
14+
15+ test ( "should return '2nd' for 2" , ( ) => {
16+ expect ( getOrdinalNumber ( 2 ) ) . toEqual ( "2nd" ) ;
17+ } ) ;
18+
19+ test ( "should return '3rd' for 3" , ( ) => {
20+ expect ( getOrdinalNumber ( 3 ) ) . toEqual ( "3rd" ) ;
21+ } ) ;
22+
23+ test ( "should return '4th' for 4" , ( ) => {
24+ expect ( getOrdinalNumber ( 4 ) ) . toEqual ( "4th" ) ;
25+ } ) ;
26+
27+ test ( "should return '11th' for 11" , ( ) => {
28+ expect ( getOrdinalNumber ( 11 ) ) . toEqual ( "11th" ) ;
29+ } ) ;
30+
31+ test ( "should return '12th' for 12" , ( ) => {
32+ expect ( getOrdinalNumber ( 12 ) ) . toEqual ( "12th" ) ;
33+ } ) ;
34+
35+ test ( "should return '13th' for 13" , ( ) => {
36+ expect ( getOrdinalNumber ( 13 ) ) . toEqual ( "13th" ) ;
37+ } ) ;
38+
39+ test ( "should return '111th' for 111" , ( ) => {
40+ expect ( getOrdinalNumber ( 111 ) ) . toEqual ( "111th" ) ;
41+ } ) ;
You can’t perform that action at this time.
0 commit comments