@@ -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+ test ( "when given an empty array to the function it returns Infinity" , ( ) => {
20+ const elements = [ ] ;
21+
22+ const result = findMax ( elements ) ;
23+ expect ( result ) . toEqual ( Infinity ) ;
24+ } ) ;
2025
2126// Given an array with one number
2227// When passed to the max function
2328// Then it should return that number
24-
29+ test ( "when the given is array of one number it should return that number" , ( ) => {
30+ const elements = [ 30 ] ;
31+ const result = findMax ( elements ) ;
32+ expect ( result ) . toEqual ( 30 ) ;
33+ } ) ;
2534// Given an array with both positive and negative numbers
2635// When passed to the max function
2736// Then it should return the largest number overall
28-
37+ test ( "should return the largest number when the array contains both negative and positive numbers" , ( ) => {
38+ const elements = [ - 10 , - 12 , - 7 , 0 , 2 , 4 ] ;
39+ const result = findMax ( elements ) ;
40+ expect ( result ) . toEqual ( 4 ) ;
41+ } ) ;
42+ test ( "should return the largest number when the array contains both negative and positive numbers" , ( ) => {
43+ const elements = [ - 20 , - 6 , - 2 , 0 , 7 , 9 , 5 ] ;
44+ const result = findMax ( elements ) ;
45+ expect ( result ) . toEqual ( 9 ) ;
46+ } ) ;
2947// Given an array with just negative numbers
3048// When passed to the max function
3149// Then it should return the closest one to zero
32-
50+ test ( "should return number that is closest to zero when the array contains all negative numbers" , ( ) => {
51+ const elements = [ - 20 , - 6 , - 2 , - 4 , - 7 , - 9 , - 5 ] ;
52+ const result = findMax ( elements ) ;
53+ expect ( result ) . toEqual ( - 2 ) ;
54+ } ) ;
55+ test ( "should return number that is closest to zero when the array contains all negative numbers" , ( ) => {
56+ const elements = [ - 20 , - 11 , - 1 , 0 , - 7 , - 9 , - 5 ] ;
57+ const result = findMax ( elements ) ;
58+ expect ( result ) . toEqual ( 0 ) ;
59+ } ) ;
3360// Given an array with decimal numbers
3461// When passed to the max function
3562// Then it should return the largest decimal number
36-
63+ test ( "should return the largest decimal number when the array contains decimal numbers" , ( ) => {
64+ const elements = [ 2.7 , 6.3 , 7.3 , 4.9 , 7.9 , 4.5 ] ;
65+ const result = findMax ( elements ) ;
66+ expect ( result ) . toEqual ( 7.9 ) ;
67+ } ) ;
68+ test ( "should return the largest decimal number when the array contains decimal numbers" , ( ) => {
69+ const elements = [ 11.8 , 16.3 , 27.3 , 41.9 , 0.9 , 14.5 ] ;
70+ const result = findMax ( elements ) ;
71+ expect ( result ) . toEqual ( 41.9 ) ;
72+ } ) ;
3773// Given an array with non-number values
3874// When passed to the max function
3975// Then it should return the max and ignore non-numeric values
40-
76+ test ( "should return the largest number ignoring non-numerical values when the array contains non-numerical value" , ( ) => {
77+ const elements = [ 11 , "apple" , 27 , "hi" , 9 , "zero" ] ;
78+ const result = findMax ( elements ) ;
79+ expect ( result ) . toEqual ( 27 ) ;
80+ } ) ;
4181// Given an array with only non-number values
4282// When passed to the max function
4383// Then it should return the least surprising value given how it behaves for all other inputs
84+ test ( "should return only number value, when the array contains non-numerical value" , ( ) => {
85+ const elements = [ "apple" , "hi" , "book" , "zero" ] ;
86+ const result = findMax ( elements ) ;
87+ expect ( result ) . toEqual ( undefined ) ;
88+ } ) ;
0 commit comments