@@ -12,32 +12,68 @@ We have set things up already so that this file can see your function from the o
1212
1313const findMax = require ( "./max.js" ) ;
1414
15- // Given an empty array
16- // When passed to the max function
17- // Then it should return -Infinity
18- // Delete this test.todo and replace it with a test.
19- test . todo ( "given an empty array, returns -Infinity" ) ;
20-
21- // Given an array with one number
22- // When passed to the max function
23- // Then it should return that number
24-
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
28-
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
32-
33- // Given an array with decimal numbers
34- // When passed to the max function
35- // Then it should return the largest decimal number
36-
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
40-
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
15+ describe ( "max()" , ( ) => {
16+ it ( "returns -Infinity for empty array" , ( ) =>
17+ expect ( findMax ( [ ] ) ) . toBe ( - Infinity ) ) ;
18+
19+ [
20+ { input : [ 43 ] , expected : 43 } ,
21+ { input : [ 342 ] , expected : 342 } ,
22+ { input : [ 65455453 ] , expected : 65455453 } ,
23+ ] . forEach ( ( { input, expected } ) =>
24+ it ( "returns the only number in array with one number" , ( ) =>
25+ expect ( findMax ( input ) ) . toBe ( expected ) )
26+ ) ;
27+
28+ [
29+ { input : [ 43 , - 4 ] , expected : 43 } ,
30+ { input : [ 342 , - 45 , - 768 , 23 ] , expected : 342 } ,
31+ { input : [ 65455453 , - 54666 , - 4566 , 6565 ] , expected : 65455453 } ,
32+ ] . forEach ( ( { input, expected } ) =>
33+ it ( "returns the largest number in array containing negative numbers" , ( ) =>
34+ expect ( findMax ( input ) ) . toBe ( expected ) )
35+ ) ;
36+
37+ [
38+ { input : [ - 43 , - 4 ] , expected : - 4 } ,
39+ { input : [ - 342 , - 45 , - 768 , - 23 ] , expected : - 23 } ,
40+ { input : [ - 65455453 , - 54666 , - 4566 , - 6565 ] , expected : - 4566 } ,
41+ ] . forEach ( ( { input, expected } ) =>
42+ it ( "returns closes number to zero in an array with only negative numbers" , ( ) =>
43+ expect ( findMax ( input ) ) . toBe ( expected ) )
44+ ) ;
45+
46+ [
47+ { input : [ 43.32 , - 4.1 ] , expected : 43.32 } ,
48+ { input : [ 342.54 , - 45.12 , - 768.76 , 23.99 ] , expected : 342.54 } ,
49+ {
50+ input : [ 65455453.4533 , - 54666.222 , - 4566.322 , 6565.43 ] ,
51+ expected : 65455453.4533 ,
52+ } ,
53+ ] . forEach ( ( { input, expected } ) =>
54+ it ( "returns the largest decimal number in an array with numbers" , ( ) =>
55+ expect ( findMax ( input ) ) . toBe ( expected ) )
56+ ) ;
57+
58+ [
59+ { input : [ 1 , 2 , "3" , null , undefined , 4 ] , expected : 4 } ,
60+ { input : [ "apple" , 1 , 2 , 34 , "banana" , 4 ] , expected : 34 } ,
61+ { input : [ 1 , "2" , 3 , "4" , 5 ] , expected : 5 } ,
62+ { input : [ 1 , "apple" , 2 , null , 3 , undefined , 4 ] , expected : 4 } ,
63+ { input : [ 3 , "apple" , 1 , null , 2 , undefined , 4 , 54 ] , expected : 54 } ,
64+ { input : [ "banana" , 5 , 3 , "apple" , 1 , 4 , 2 ] , expected : 5 } ,
65+ ] . forEach ( ( { input, expected } ) =>
66+ it ( "returns max of numbers in an array containing non-numeric values" , ( ) =>
67+ expect ( findMax ( input ) ) . toEqual ( expected ) )
68+ ) ;
69+
70+ [
71+ [ "not an array" , "3" , null , undefined ] ,
72+ [ "apple" , "banana" ] ,
73+ [ "apple" , null , undefined ] ,
74+ [ "banana" , { } ] ,
75+ ] . forEach ( ( input ) =>
76+ it ( "returns -Infinity in array with only non-numeric values" , ( ) =>
77+ expect ( findMax ( input ) ) . toBe ( - Infinity ) )
78+ ) ;
79+ } ) ;
0 commit comments