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