1- /* Find the maximum element of an array of numbers
2-
3- In this kata, you will need to implement a function that find the largest numerical element of an array.
4-
5- E.g. max([30, 50, 10, 40]), target output: 50
6- E.g. max(['hey', 10, 'hi', 60, 10]), target output: 60 (sum ignores any non-numerical elements)
7-
8- You should implement this function in max.js, and add tests for it in this file.
9-
10- We have set things up already so that this file can see your function from the other file.
11- */
12-
131const findMax = require ( "./max.js" ) ;
142
153// Given an empty array
164// When passed to the max function
175// 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" ) ;
6+ test ( "given an empty array, returns -Infinity" , ( ) => {
7+ expect ( findMax ( [ ] ) ) . toBe ( - Infinity ) ;
8+ } ) ;
209
2110// Given an array with one number
2211// When passed to the max function
2312// Then it should return that number
13+ test ( "given an array with one number" , ( ) => {
14+ expect ( findMax ( [ 5 ] ) ) . toBe ( 5 ) ;
15+ } ) ;
2416
2517// Given an array with both positive and negative numbers
2618// When passed to the max function
2719// Then it should return the largest number overall
20+ test ( "given positive and negative numbers" , ( ) => {
21+ expect ( findMax ( [ - 10 , 20 , 5 , - 2 ] ) ) . toBe ( 20 ) ;
22+ } ) ;
2823
2924// Given an array with just negative numbers
3025// When passed to the max function
3126// Then it should return the closest one to zero
27+ test ( "given only negative numbers" , ( ) => {
28+ expect ( findMax ( [ - 10 , - 3 , - 50 , - 1 ] ) ) . toBe ( - 1 ) ;
29+ } ) ;
3230
3331// Given an array with decimal numbers
3432// When passed to the max function
3533// Then it should return the largest decimal number
34+ test ( "given decimal numbers" , ( ) => {
35+ expect ( findMax ( [ 1.2 , 5.7 , 3.3 ] ) ) . toBe ( 5.7 ) ;
36+ } ) ;
3637
3738// Given an array with non-number values
3839// When passed to the max function
3940// Then it should return the max and ignore non-numeric values
41+ test ( "ignores non-number values" , ( ) => {
42+ expect ( findMax ( [ "hey" , 10 , "hi" , 60 , 10 ] ) ) . toBe ( 60 ) ;
43+ } ) ;
4044
4145// Given an array with only non-number values
4246// When passed to the max function
43- // Then it should return the least surprising value given how it behaves for all other inputs
47+ // Then it should return the least surprising value
48+ test ( "only non-number values returns -Infinity" , ( ) => {
49+ expect ( findMax ( [ "a" , "b" , "c" ] ) ) . toBe ( - Infinity ) ;
50+ } ) ;
0 commit comments