1- // median.test.js
2-
3- // Someone has implemented calculateMedian but it isn't
4- // passing all the tests...
5- // Fix the implementation of calculateMedian so it passes all tests
6-
71const calculateMedian = require ( "./median.js" ) ;
82
93describe ( "calculateMedian" , ( ) => {
@@ -13,7 +7,8 @@ describe("calculateMedian", () => {
137 { input : [ 1 , 2 , 3 , 4 ] , expected : 2.5 } ,
148 { input : [ 1 , 2 , 3 , 4 , 5 , 6 ] , expected : 3.5 } ,
159 ] . forEach ( ( { input, expected } ) =>
16- it ( `returns the median for [${ input } ]` , ( ) => expect ( calculateMedian ( input ) ) . toEqual ( expected ) )
10+ it ( `returns the median for [${ input } ]` , ( ) =>
11+ expect ( calculateMedian ( input ) ) . toEqual ( expected ) )
1712 ) ;
1813
1914 [
@@ -24,7 +19,8 @@ describe("calculateMedian", () => {
2419 { input : [ 110 , 20 , 0 ] , expected : 20 } ,
2520 { input : [ 6 , - 2 , 2 , 12 , 14 ] , expected : 6 } ,
2621 ] . forEach ( ( { input, expected } ) =>
27- it ( `returns the correct median for unsorted array [${ input } ]` , ( ) => expect ( calculateMedian ( input ) ) . toEqual ( expected ) )
22+ it ( `returns the correct median for unsorted array [${ input } ]` , ( ) =>
23+ expect ( calculateMedian ( input ) ) . toEqual ( expected ) )
2824 ) ;
2925
3026 it ( "doesn't modify the input array [3, 1, 2]" , ( ) => {
@@ -33,8 +29,9 @@ describe("calculateMedian", () => {
3329 expect ( list ) . toEqual ( [ 3 , 1 , 2 ] ) ;
3430 } ) ;
3531
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 ) )
32+ [ 'not an array' , 123 , null , undefined , { } , [ ] , [ "apple" , null , undefined ] ] . forEach ( val =>
33+ it ( `returns null for non-numeric array (${ val } )` , ( ) =>
34+ expect ( calculateMedian ( val ) ) . toBe ( null ) )
3835 ) ;
3936
4037 [
@@ -45,6 +42,12 @@ describe("calculateMedian", () => {
4542 { input : [ 3 , "apple" , 1 , null , 2 , undefined , 4 ] , expected : 2.5 } ,
4643 { input : [ "banana" , 5 , 3 , "apple" , 1 , 4 , 2 ] , expected : 3 } ,
4744 ] . forEach ( ( { input, expected } ) =>
48- it ( `filters out non-numeric values and calculates the median for [${ input } ]` , ( ) => expect ( calculateMedian ( input ) ) . toEqual ( expected ) )
45+ it ( `filters out non-numeric values and calculates the median for [${ input } ]` , ( ) =>
46+ expect ( calculateMedian ( input ) ) . toEqual ( expected ) )
4947 ) ;
50- } ) ;
48+
49+ // 🔥 EXTRA test for reviewer feedback
50+ it ( "ignores NaN and Infinity values" , ( ) => {
51+ expect ( calculateMedian ( [ 1 , 2 , NaN , Infinity , 3 ] ) ) . toEqual ( 2 ) ;
52+ } ) ;
53+ } ) ;
0 commit comments