77const calculateMedian = require ( "./median.js" ) ;
88
99describe ( "calculateMedian" , ( ) => {
10- [
11- { input : [ 1 , 2 , 3 ] , expected : 2 } ,
12- { input : [ 1 , 2 , 3 , 4 , 5 ] , expected : 3 } ,
13- { input : [ 1 , 2 , 3 , 4 ] , expected : 2.5 } ,
14- { input : [ 1 , 2 , 3 , 4 , 5 , 6 ] , expected : 3.5 } ,
15- ] . forEach ( ( { input, expected } ) =>
16- it ( `returns the median for [${ input } ]` , ( ) => expect ( calculateMedian ( input ) ) . toEqual ( expected ) )
17- ) ;
18-
19- [
20- { input : [ 3 , 1 , 2 ] , expected : 2 } ,
21- { input : [ 5 , 1 , 3 , 4 , 2 ] , expected : 3 } ,
22- { input : [ 4 , 2 , 1 , 3 ] , expected : 2.5 } ,
23- { input : [ 6 , 1 , 5 , 3 , 2 , 4 ] , expected : 3.5 } ,
24- { input : [ 110 , 20 , 0 ] , expected : 20 } ,
25- { input : [ 6 , - 2 , 2 , 12 , 14 ] , expected : 6 } ,
26- ] . forEach ( ( { input, expected } ) =>
27- it ( `returns the correct median for unsorted array [${ input } ]` , ( ) => expect ( calculateMedian ( input ) ) . toEqual ( expected ) )
28- ) ;
10+ it ( "returns the median for [1, 2, 3]" , ( ) => {
11+ expect ( calculateMedian ( [ 1 , 2 , 3 ] ) ) . toEqual ( 2 ) ;
12+ } ) ;
13+
14+ it ( "returns the median for [1, 2, 3, 4, 5]" , ( ) => {
15+ expect ( calculateMedian ( [ 1 , 2 , 3 , 4 , 5 ] ) ) . toEqual ( 3 ) ;
16+ } ) ;
17+
18+ it ( "returns the median for [1, 2, 3, 4]" , ( ) => {
19+ expect ( calculateMedian ( [ 1 , 2 , 3 , 4 ] ) ) . toEqual ( 2.5 ) ;
20+ } ) ;
21+
22+ it ( "returns the median for [1, 2, 3, 4, 5, 6]" , ( ) => {
23+ expect ( calculateMedian ( [ 1 , 2 , 3 , 4 , 5 , 6 ] ) ) . toEqual ( 3.5 ) ;
24+ } ) ;
25+
26+ it ( "returns the correct median for unsorted array [3, 1, 2]" , ( ) => {
27+ expect ( calculateMedian ( [ 3 , 1 , 2 ] ) ) . toEqual ( 2 ) ;
28+ } ) ;
29+
30+ it ( "returns the correct median for unsorted array [5, 1, 3, 4, 2]" , ( ) => {
31+ expect ( calculateMedian ( [ 5 , 1 , 3 , 4 , 2 ] ) ) . toEqual ( 3 ) ;
32+ } ) ;
33+
34+ it ( "returns the correct median for unsorted array [4, 2, 1, 3]" , ( ) => {
35+ expect ( calculateMedian ( [ 4 , 2 , 1 , 3 ] ) ) . toEqual ( 2.5 ) ;
36+ } ) ;
37+
38+ it ( "returns the correct median for unsorted array [6, 1, 5, 3, 2, 4]" , ( ) => {
39+ expect ( calculateMedian ( [ 6 , 1 , 5 , 3 , 2 , 4 ] ) ) . toEqual ( 3.5 ) ;
40+ } ) ;
41+
42+ it ( "returns the correct median for unsorted array [110, 20, 0]" , ( ) => {
43+ expect ( calculateMedian ( [ 110 , 20 , 0 ] ) ) . toEqual ( 20 ) ;
44+ } ) ;
45+
46+ it ( "returns the correct median for unsorted array [6, -2, 2, 12, 14]" , ( ) => {
47+ expect ( calculateMedian ( [ 6 , - 2 , 2 , 12 , 14 ] ) ) . toEqual ( 6 ) ;
48+ } ) ;
2949
3050 it ( "doesn't modify the input array [3, 1, 2]" , ( ) => {
3151 const list = [ 3 , 1 , 2 ] ;
@@ -40,13 +60,30 @@ describe("calculateMedian", () => {
4060 ) ;
4161 } ) ;
4262
43- // Non-array input should throw (no argument at all counts, as 'list' is then undefined)
44- [ 'banana' , 123 , null , undefined , { } , [ "apple" , null , undefined ] , [ "ten" , "twenty" ] ] . forEach ( val =>
45- it ( `throws for non-numeric input (${ val } )` , ( ) =>
46- expect ( ( ) => calculateMedian ( val ) ) . toThrow (
47- new Error ( "calculateMedian requires an array of numbers" )
48- ) )
49- ) ;
63+ // Input that isn't an array should throw
64+ it ( "throws when given a string" , ( ) => {
65+ expect ( ( ) => calculateMedian ( "banana" ) ) . toThrow (
66+ new Error ( "calculateMedian requires an array of numbers" )
67+ ) ;
68+ } ) ;
69+
70+ it ( "throws when given a number" , ( ) => {
71+ expect ( ( ) => calculateMedian ( 123 ) ) . toThrow (
72+ new Error ( "calculateMedian requires an array of numbers" )
73+ ) ;
74+ } ) ;
75+
76+ it ( "throws when given null" , ( ) => {
77+ expect ( ( ) => calculateMedian ( null ) ) . toThrow (
78+ new Error ( "calculateMedian requires an array of numbers" )
79+ ) ;
80+ } ) ;
81+
82+ it ( "throws when given an object" , ( ) => {
83+ expect ( ( ) => calculateMedian ( { } ) ) . toThrow (
84+ new Error ( "calculateMedian requires an array of numbers" )
85+ ) ;
86+ } ) ;
5087
5188 it ( "throws when called with no argument" , ( ) => {
5289 expect ( ( ) => calculateMedian ( ) ) . toThrow (
@@ -55,17 +92,27 @@ describe("calculateMedian", () => {
5592 } ) ;
5693
5794 // Arrays containing any non-number value should throw, rather than filtering them out
58- [
59- [ 1 , 2 , "3" , null , undefined , 4 ] ,
60- [ "apple" , 1 , 2 , 3 , "banana" , 4 ] ,
61- [ 1 , "2" , 3 , "4" , 5 ] ,
62- [ 1 , "apple" , 2 , null , 3 , undefined , 4 ] ,
63- [ 3 , "apple" , 1 , null , 2 , undefined , 4 ] ,
64- [ "banana" , 5 , 3 , "apple" , 1 , 4 , 2 ] ,
65- ] . forEach ( ( input ) =>
66- it ( `throws for an array containing a non-number [${ input } ]` , ( ) =>
67- expect ( ( ) => calculateMedian ( input ) ) . toThrow (
68- new Error ( "calculateMedian requires an array of numbers" )
69- ) )
70- ) ;
95+ it ( "throws for an array of strings" , ( ) => {
96+ expect ( ( ) => calculateMedian ( [ "ten" , "twenty" , "thirty" ] ) ) . toThrow (
97+ new Error ( "calculateMedian requires an array of numbers" )
98+ ) ;
99+ } ) ;
100+
101+ it ( "throws for an array mixing numbers and strings" , ( ) => {
102+ expect ( ( ) => calculateMedian ( [ 1 , "2" , 3 , "4" , 5 ] ) ) . toThrow (
103+ new Error ( "calculateMedian requires an array of numbers" )
104+ ) ;
105+ } ) ;
106+
107+ it ( "throws for an array containing null" , ( ) => {
108+ expect ( ( ) => calculateMedian ( [ 1 , 2 , null , 4 ] ) ) . toThrow (
109+ new Error ( "calculateMedian requires an array of numbers" )
110+ ) ;
111+ } ) ;
112+
113+ it ( "throws for an array containing undefined" , ( ) => {
114+ expect ( ( ) => calculateMedian ( [ 3 , 1 , undefined , 2 ] ) ) . toThrow (
115+ new Error ( "calculateMedian requires an array of numbers" )
116+ ) ;
117+ } ) ;
71118} ) ;
0 commit comments