@@ -18,3 +18,64 @@ test("should append 'st' for numbers ending with 1, except those ending with 11"
1818 expect ( getOrdinalNumber ( 21 ) ) . toEqual ( "21st" ) ;
1919 expect ( getOrdinalNumber ( 131 ) ) . toEqual ( "131st" ) ;
2020} ) ;
21+
22+ // Case 2: Numbers ending with 2 (but not 12)
23+ // When the number ends with 2, except those ending with 12,
24+ // Then the function should return a string by appending "nd".
25+ test ( "should append 'nd' for numbers ending with 2, except those ending with 12" , ( ) => {
26+ expect ( getOrdinalNumber ( 2 ) ) . toEqual ( "2nd" ) ;
27+ expect ( getOrdinalNumber ( 22 ) ) . toEqual ( "22nd" ) ;
28+ expect ( getOrdinalNumber ( 142 ) ) . toEqual ( "142nd" ) ;
29+ } ) ;
30+
31+ // Case 3: Numbers ending with 3 (but not 13)
32+ // When the number ends with 3, except those ending with 13,
33+ // Then the function should return a string by appending 'rd'.
34+ test ( "should append 'rd' for numbers ending with 3, except those ending with 13" , ( ) => {
35+ expect ( getOrdinalNumber ( 3 ) ) . toEqual ( "3rd" ) ;
36+ expect ( getOrdinalNumber ( 33 ) ) . toEqual ( "33rd" ) ;
37+ expect ( getOrdinalNumber ( 153 ) ) . toEqual ( "153rd" ) ;
38+ } ) ;
39+
40+ // Case 4: Special cases 11, 12, 13
41+ // When the number ends with 11, 12, or 13,
42+ // Then the function should always append "th".
43+ test ( "should append 'th' for special cases 11, 12, 13" , ( ) => {
44+ expect ( getOrdinalNumber ( 11 ) ) . toEqual ( "11th" ) ;
45+ expect ( getOrdinalNumber ( 12 ) ) . toEqual ( "12th" ) ;
46+ expect ( getOrdinalNumber ( 13 ) ) . toEqual ( "13th" ) ;
47+ expect ( getOrdinalNumber ( 111 ) ) . toEqual ( "111th" ) ;
48+ expect ( getOrdinalNumber ( 212 ) ) . toEqual ( "212th" ) ;
49+ } ) ;
50+
51+ // Case 5: All other numbers
52+ // When the number does not end with 1, 2, or 3,
53+ // Then the function should append "th".
54+ test ( "should append 'th' for all other numbers" , ( ) => {
55+ expect ( getOrdinalNumber ( 4 ) ) . toEqual ( "4th" ) ;
56+ expect ( getOrdinalNumber ( 10 ) ) . toEqual ( "10th" ) ;
57+ expect ( getOrdinalNumber ( 100 ) ) . toEqual ( "100th" ) ;
58+ expect ( getOrdinalNumber ( 204 ) ) . toEqual ( "204th" ) ;
59+ } ) ;
60+
61+ // Case 6: Float numbers should be rounded to nearest integer
62+ // When the input is a float,
63+ // Then the function should round it and return the correct ordinal.
64+ test ( "should round float numbers and return correct ordinal" , ( ) => {
65+ expect ( getOrdinalNumber ( 1.2 ) ) . toEqual ( "1st" ) ; // rounds to 1
66+ expect ( getOrdinalNumber ( 1.8 ) ) . toEqual ( "2nd" ) ; // rounds to 2
67+ expect ( getOrdinalNumber ( 2.5 ) ) . toEqual ( "3rd" ) ; // rounds to 3
68+ expect ( getOrdinalNumber ( 10.51 ) ) . toEqual ( "11th" ) ; // rounds to 11
69+ expect ( getOrdinalNumber ( 12.49 ) ) . toEqual ( "12th" ) ; // rounds to 12
70+ expect ( getOrdinalNumber ( 12.5 ) ) . toEqual ( "13th" ) ; // rounds to 13
71+ } ) ;
72+
73+ // Case 7: Invalid inputs should throw an error
74+ // When the input is not a number,
75+ // Then the function should throw an error.
76+ test ( "should throw an error for invalid inputs" , ( ) => {
77+ expect ( ( ) => getOrdinalNumber ( "10" ) ) . toThrow ( "Invalid input" ) ;
78+ expect ( ( ) => getOrdinalNumber ( null ) ) . toThrow ( "Invalid input" ) ;
79+ expect ( ( ) => getOrdinalNumber ( undefined ) ) . toThrow ( "Invalid input" ) ;
80+ expect ( ( ) => getOrdinalNumber ( NaN ) ) . toThrow ( "Invalid input" ) ;
81+ } ) ;
0 commit comments