File tree Expand file tree Collapse file tree 2 files changed +42
-1
lines changed
Expand file tree Collapse file tree 2 files changed +42
-1
lines changed Original file line number Diff line number Diff line change 11function getOrdinalNumber ( num ) {
2- return "1st" ;
2+ const lastTwo = num % 100 ;
3+ const last = num % 10 ;
4+
5+ // Special exceptions: 11, 12, 13
6+ if ( lastTwo === 11 || lastTwo === 12 || lastTwo === 13 ) {
7+ return `${ num } th` ;
8+ }
9+
10+ // Regular cases
11+ if ( last === 1 ) return `${ num } st` ;
12+ if ( last === 2 ) return `${ num } nd` ;
13+ if ( last === 3 ) return `${ num } rd` ;
14+
15+ return `${ num } th` ;
316}
417
518module . 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+ //identify ordinal for 2
16+ test ( "should return '2nd' for 2" , ( ) => {
17+ expect ( getOrdinalNumber ( 2 ) ) . toEqual ( "2nd" ) ;
18+ } ) ;
19+
20+ //identify ordinal for 3
21+ test ( "should return '3rd' for 3" , ( ) => {
22+ expect ( getOrdinalNumber ( 3 ) ) . toEqual ( "3rd" ) ;
23+ } ) ;
24+
25+ //identify ordinal for 4
26+ test ( "should return '4th' for 4" , ( ) => {
27+ expect ( getOrdinalNumber ( 4 ) ) . toEqual ( "4th" ) ;
28+ } ) ;
29+
30+ //Special English Rule... 12, 13, 14 ending in "th"
31+ test ( "should return '11th' for 11" , ( ) => {
32+ expect ( getOrdinalNumber ( 11 ) ) . toEqual ( "11th" ) ;
33+ } ) ;
34+
35+ test ( "should return '12th' for 12" , ( ) => {
36+ expect ( getOrdinalNumber ( 12 ) ) . toEqual ( "12th" ) ;
37+ } ) ;
38+
39+ test ( "should return '13th' for 13" , ( ) => {
40+ expect ( getOrdinalNumber ( 13 ) ) . toEqual ( "13th" ) ;
41+ } ) ;
You can’t perform that action at this time.
0 commit comments