@@ -13,7 +13,8 @@ describe("calculateMedian", () => {
1313 { input : [ 1 , 2 , 3 , 4 ] , expected : 2.5 } ,
1414 { input : [ 1 , 2 , 3 , 4 , 5 , 6 ] , expected : 3.5 } ,
1515 ] . forEach ( ( { input, expected } ) =>
16- it ( `returns the median for [${ input } ]` , ( ) => expect ( calculateMedian ( input ) ) . toEqual ( expected ) )
16+ it ( `returns the median for [${ input } ]` , ( ) =>
17+ expect ( calculateMedian ( input ) ) . toEqual ( expected ) )
1718 ) ;
1819
1920 [
@@ -24,7 +25,8 @@ describe("calculateMedian", () => {
2425 { input : [ 110 , 20 , 0 ] , expected : 20 } ,
2526 { input : [ 6 , - 2 , 2 , 12 , 14 ] , expected : 6 } ,
2627 ] . forEach ( ( { input, expected } ) =>
27- it ( `returns the correct median for unsorted array [${ input } ]` , ( ) => expect ( calculateMedian ( input ) ) . toEqual ( expected ) )
28+ it ( `returns the correct median for unsorted array [${ input } ]` , ( ) =>
29+ expect ( calculateMedian ( input ) ) . toEqual ( expected ) )
2830 ) ;
2931
3032 it ( "doesn't modify the input array [3, 1, 2]" , ( ) => {
@@ -33,8 +35,17 @@ describe("calculateMedian", () => {
3335 expect ( list ) . toEqual ( [ 3 , 1 , 2 ] ) ;
3436 } ) ;
3537
36- [ 'not an array' , 123 , null , undefined , { } , [ ] , [ "apple" , null , undefined ] ] . forEach ( val =>
37- it ( `returns null for non-numeric array (${ val } )` , ( ) => expect ( calculateMedian ( val ) ) . toBe ( null ) )
38+ [
39+ "not an array" ,
40+ 123 ,
41+ null ,
42+ undefined ,
43+ { } ,
44+ [ ] ,
45+ [ "apple" , null , undefined ] ,
46+ ] . forEach ( ( val ) =>
47+ it ( `returns null for non-numeric array (${ val } )` , ( ) =>
48+ expect ( calculateMedian ( val ) ) . toBe ( null ) )
3849 ) ;
3950
4051 [
@@ -45,6 +56,33 @@ describe("calculateMedian", () => {
4556 { input : [ 3 , "apple" , 1 , null , 2 , undefined , 4 ] , expected : 2.5 } ,
4657 { input : [ "banana" , 5 , 3 , "apple" , 1 , 4 , 2 ] , expected : 3 } ,
4758 ] . forEach ( ( { input, expected } ) =>
48- it ( `filters out non-numeric values and calculates the median for [${ input } ]` , ( ) => expect ( calculateMedian ( input ) ) . toEqual ( expected ) )
59+ it ( `filters out non-numeric values and calculates the median for [${ input } ]` , ( ) =>
60+ expect ( calculateMedian ( input ) ) . toEqual ( expected ) )
61+ ) ;
62+ } ) ;
63+
64+ describe ( "calculateMedian - ignores non-finite numbers" , ( ) => {
65+ [
66+ { input : [ 1 , 2 , NaN , 3 ] , expected : 2 } ,
67+ { input : [ 1 , 2 , Infinity , 3 ] , expected : 2 } ,
68+ { input : [ 1 , 2 , - Infinity , 3 ] , expected : 2 } ,
69+ { input : [ 1 , 2 , NaN , Infinity , - Infinity , 3 ] , expected : 2 } ,
70+ { input : [ NaN , Infinity , - Infinity , 5 , 10 ] , expected : 7.5 } ,
71+ ] . forEach ( ( { input, expected } ) =>
72+ it ( `ignores NaN/Infinity values in [${ input } ]` , ( ) => {
73+ expect ( calculateMedian ( input ) ) . toEqual ( expected ) ;
74+ } )
4975 ) ;
76+
77+ it ( "returns null when all values are non-finite" , ( ) => {
78+ expect ( calculateMedian ( [ NaN , Infinity , - Infinity ] ) ) . toBe ( null ) ;
79+ } ) ;
80+
81+ it ( "handles mix of finite and non-finite with even count" , ( ) => {
82+ expect ( calculateMedian ( [ 1 , 2 , 3 , 4 , Infinity , NaN ] ) ) . toBe ( 2.5 ) ;
83+ } ) ;
84+
85+ it ( "handles mix of finite and non-finite with odd count" , ( ) => {
86+ expect ( calculateMedian ( [ 1 , 2 , 3 , Infinity , NaN ] ) ) . toBe ( 2 ) ;
87+ } ) ;
5088} ) ;
0 commit comments