File tree Expand file tree Collapse file tree 2 files changed +43
-5
lines changed
Expand file tree Collapse file tree 2 files changed +43
-5
lines changed Original file line number Diff line number Diff line change 11function getOrdinalNumber ( num ) {
2- return "1st" ;
2+ if ( num % 100 >= 11 && num % 100 <= 13 ) {
3+ return `${ num } th` ;
4+ }
5+ const lastDigit = num % 10 ;
6+ let suffix = "th" ;
7+
8+ switch ( lastDigit ) {
9+ case 1 :
10+ suffix = "st" ;
11+ break ;
12+ case 2 :
13+ suffix = "nd" ;
14+ break ;
15+ case 3 :
16+ suffix = "rd" ;
17+ break ;
18+ }
19+
20+ return `${ num } ${ suffix } ` ;
321}
422
523module . exports = getOrdinalNumber ;
Original file line number Diff line number Diff line change @@ -4,10 +4,30 @@ const getOrdinalNumber = require("./get-ordinal-number");
44// continue testing and implementing getOrdinalNumber for additional cases
55// Write your tests using Jest - remember to run your tests often for continual feedback
66
7- // Case 1: Identify the ordinal number for 1
8- // When the number is 1,
9- // Then the function should return "1st"
10-
117test ( "should return '1st' for 1" , ( ) => {
128 expect ( getOrdinalNumber ( 1 ) ) . toEqual ( "1st" ) ;
139} ) ;
10+
11+ test ( 'should return "2nd" for 2' , ( ) => {
12+ expect ( getOrdinalNumber ( 2 ) ) . toEqual ( "2nd" ) ;
13+ } ) ;
14+
15+ test ( "should return '3rd' for 3" , ( ) => {
16+ expect ( getOrdinalNumber ( 3 ) ) . toEqual ( "3rd" ) ;
17+ } ) ;
18+
19+ test ( 'should return "13th" for 13' , ( ) => {
20+ expect ( getOrdinalNumber ( 13 ) ) . toEqual ( "13th" ) ;
21+ } ) ;
22+
23+ test ( "should return '41st' for 41" , ( ) => {
24+ expect ( getOrdinalNumber ( 41 ) ) . toEqual ( "41st" ) ;
25+ } ) ;
26+
27+ test ( 'should return "212th" for 212' , ( ) => {
28+ expect ( getOrdinalNumber ( 212 ) ) . toEqual ( "212th" ) ;
29+ } ) ;
30+
31+ test ( "should return '221st' for 221" , ( ) => {
32+ expect ( getOrdinalNumber ( 221 ) ) . toEqual ( "221st" ) ;
33+ } ) ;
You can’t perform that action at this time.
0 commit comments