@@ -16,28 +16,73 @@ const findMax = require("./max.js");
1616// When passed to the max function
1717// Then it should return -Infinity
1818// Delete this test.todo and replace it with a test.
19- test . todo ( "given an empty array, returns -Infinity" ) ;
19+ describe ( "findMax()" , ( ) => {
20+ [ { input : [ ] , expected : Infinity } ] . forEach ( ( { input, expected } ) =>
21+ it ( `should return ${ expected } for empty [${ input } ]` , ( ) => {
22+ expect ( findMax ( input ) ) . toEqual ( expected ) ;
23+ } )
24+ ) ;
2025
21- // Given an array with one number
22- // When passed to the max function
23- // Then it should return that number
26+ // Given an array with one number
27+ // When passed to the max function
28+ // Then it should return that number
2429
25- // Given an array with both positive and negative numbers
26- // When passed to the max function
27- // Then it should return the largest number overall
30+ [ { input : [ 50 ] , expected : 50 } ] . forEach ( ( { input, expected } ) =>
31+ it ( `should return ${ expected } for array [${ input } ]` , ( ) => {
32+ expect ( findMax ( input ) ) . toEqual ( expected ) ;
33+ } )
34+ ) ;
2835
29- // Given an array with just negative numbers
30- // When passed to the max function
31- // Then it should return the closest one to zero
36+ // Given an array with both positive and negative numbers
37+ // When passed to the max function
38+ // Then it should return the largest number overall
3239
33- // Given an array with decimal numbers
34- // When passed to the max function
35- // Then it should return the largest decimal number
40+ [ { input : [ 2 , 5 , 6 , - 1 , 0 , 25 , - 30 ] , expected : 25 } ] . forEach (
41+ ( { input, expected } ) => {
42+ it ( `should return ${ expected } for positive and negative numbers in the array` , ( ) => {
43+ expect ( findMax ( input ) ) . toEqual ( expected ) ;
44+ } ) ;
45+ }
46+ ) ;
3647
37- // Given an array with non-number values
38- // When passed to the max function
39- // Then it should return the max and ignore non-numeric values
48+ // Given an array with just negative numbers
49+ // When passed to the max function
50+ // Then it should return the closest one to zero
51+ [ { input : [ - 1 , - 10 , - 7 , - 20 ] , expected : - 1 } ] . forEach ( ( { input, expected } ) =>
52+ it ( `should return negative number nearest to zero` , ( ) => {
53+ expect ( findMax ( input ) ) . toEqual ( expected ) ;
54+ } )
55+ ) ;
4056
41- // Given an array with only non-number values
42- // When passed to the max function
43- // Then it should return the least surprising value given how it behaves for all other inputs
57+ // Given an array with decimal numbers
58+ // When passed to the max function
59+ // Then it should return the largest decimal number
60+
61+ [ { input : [ 0.5 , 0.1 , 0.56 , 0.8 ] , expected : 0.8 } ] . forEach (
62+ ( { input, expected } ) =>
63+ it ( `should return the largest decimal number from the array` , ( ) => {
64+ expect ( findMax ( input ) ) . toEqual ( expected ) ;
65+ } )
66+ ) ;
67+
68+ // Given an array with non-number values
69+ // When passed to the max function
70+ // Then it should return the max and ignore non-numeric values
71+ [
72+ { input : [ "edak" , "ofonime" , "" , "@" , - 4 , 10 , 6 , 50 , - 100 ] , expected : 50 } ,
73+ ] . forEach ( ( { input, expected } ) =>
74+ it ( `should return max numerical value from the array` , ( ) => {
75+ expect ( findMax ( input ) ) . toEqual ( expected ) ;
76+ } )
77+ ) ;
78+
79+ // Given an array with only non-number values
80+ // When passed to the max function
81+ // Then it should return the least surprising value given how it behaves for all other inputs
82+ [ { input : [ "peter" , "" , "@" , "Hi" ] , expected : "invalid elements" } ] . forEach (
83+ ( { input, expected } ) =>
84+ it ( `should return "invalid elements" for non-numeric values` , ( ) => {
85+ expect ( findMax ( input ) ) . toEqual ( expected ) ;
86+ } )
87+ ) ;
88+ } ) ;
0 commit comments