@@ -12,32 +12,96 @@ 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 ( "findMax" , ( ) => {
16+ // Given an empty array
17+ // When passed to the max function
18+ // Then it should return -Infinity
19+ it ( "if an empty array is passed to the the findMax function, -Infinity should be returned" , ( ) => {
20+ emptyArray = [ ] ;
21+ expect ( findMax ( emptyArray ) ) . toEqual ( - Infinity ) ;
22+ } ) ;
23+
24+ // Given an array with one number
25+ // When passed to the max function
26+ // Then it should return that number
27+ [ [ 1 ] , [ 70 ] , [ 0 ] , [ - 25 ] , [ 100129 ] ] . forEach ( ( val ) =>
28+ it ( `When an array with only one number is passed i.e. [${ val } ], it should return that number` , ( ) =>
29+ expect ( findMax ( val ) ) . toEqual ( val [ 0 ] ) )
30+ ) ;
31+
32+ // Given an array with both positive and negative numbers
33+ // When passed to the max function
34+ // Then it should return the largest number overall
35+ [
36+ { input : [ 12 , - 2 , 4 , 6 , 0 ] , expected : 12 } ,
37+ { input : [ 2 , 5 , 990 , - 4 ] , expected : 990 } ,
38+ { input : [ 0 , - 1 , - 5 ] , expected : 0 } ,
39+ { input : [ 0 , - 1 , 300 , 3 ] , expected : 300 } ,
40+ ] . forEach ( ( { input, expected } ) =>
41+ it ( `returns the max number from the array [${ input } ]` , ( ) =>
42+ expect ( findMax ( input ) ) . toEqual ( expected ) )
43+ ) ;
44+
45+ // Given an array with just negative numbers
46+ // When passed to the max function
47+ // Then it should return the closest one to zero
48+ [
49+ { input : [ - 4 , - 2 , - 1902 , - 2 , - 1 ] , expected : - 1 } ,
50+ { input : [ - 9088 , - 9087 , - 990788 , - 4888777 ] , expected : - 9087 } ,
51+ { input : [ - 1 , - 5 ] , expected : - 1 } ,
52+ ] . forEach ( ( { input, expected } ) =>
53+ it ( `returns the max number from the array of negative numbers only [${ input } ]` , ( ) =>
54+ expect ( findMax ( input ) ) . toEqual ( expected ) )
55+ ) ;
56+
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 : [ - 4.2 , 2.8 , - 19.8009 , 2.4 , 1.5 ] , expected : 2.8 } ,
62+ { input : [ - 90.88 , 0.001 , - 990.788 ] , expected : 0.001 } ,
63+ { input : [ - 1.11 , - 5.3 ] , expected : - 1.11 } ,
64+ ] . forEach ( ( { input, expected } ) =>
65+ it ( `returns the max number from the array of decimal numbers [${ input } ]` , ( ) =>
66+ expect ( findMax ( input ) ) . toEqual ( expected ) )
67+ ) ;
68+
69+ // Given an array with non-number values
70+ // When passed to the max function
71+ // Then it should return the max and ignore non-numeric values
72+ [
73+ { input : [ - 4 , - 2 , "what is this" , - 1902 , - 2 , "??" , - 1 ] , expected : - 1 } ,
74+ {
75+ input : [ - 9088 , ".oi9e9" , "1000000" , - 9087 , 990788 , - 4888777 ] ,
76+ expected : 990788 ,
77+ } ,
78+ { input : [ - 1.11 , "here" , - 5.233 , "ignore me please" ] , expected : - 1.11 } ,
79+ ] . forEach ( ( { input, expected } ) =>
80+ it ( `returns the max number from the array of numbers and non-numbers values [${ input } ]` , ( ) =>
81+ expect ( findMax ( input ) ) . toEqual ( expected ) )
82+ ) ;
83+
84+ // Given an array with only non-number values
85+ // When passed to the max function
86+ // Then it should return the least surprising value given how it behaves for all other inputs
87+
88+ /* Ans: If there is no number in the array than considering the behavior for the above inputs
89+ the least surprising value in return would be -Infinity as our array has zero elements which are numbers*/
90+ [
91+ {
92+ input : [ "Not a number" , "what is this" , "least surprising" , "??" , "what" ] ,
93+ expected : - Infinity ,
94+ } ,
95+ {
96+ input : [ "kkdkas" , "Ahan!" , "23" ] ,
97+ expected : - Infinity ,
98+ } ,
99+ {
100+ input : [ "here" , "Least surprising" , "????" , "vale is" , "-Infinity" ] ,
101+ expected : - Infinity ,
102+ } ,
103+ ] . forEach ( ( { input, expected } ) =>
104+ it ( `returns the least surprising value for only non-numbers array [${ input } ]` , ( ) =>
105+ expect ( findMax ( input ) ) . toEqual ( expected ) )
106+ ) ;
107+ } ) ;
0 commit comments