@@ -9,28 +9,46 @@ E.g. sum(['hey', 10, 'hi', 60, 10]), target output: 80 (ignore any non-numerical
99const sum = require ( "./sum.js" ) ;
1010
1111// Acceptance Criteria:
12-
13- // Given an empty array
14- // When passed to the sum function
15- // Then it should return 0
16- test . todo ( "given an empty array, returns 0" )
17-
18- // Given an array with just one number
19- // When passed to the sum function
20- // Then it should return that number
21-
22- // Given an array containing negative numbers
23- // When passed to the sum function
24- // Then it should still return the correct total sum
25-
26- // Given an array with decimal/float numbers
27- // When passed to the sum function
28- // Then it should return the correct total sum
29-
30- // Given an array containing non-number values
31- // When passed to the sum function
32- // Then it should ignore the non-numerical values and return the sum of the numerical elements
33-
34- // Given an array with only non-number values
35- // When passed to the sum function
36- // Then it should return the least surprising value given how it behaves for all other inputs
12+ describe ( "sum" , ( ) => {
13+ // Given an empty array
14+ // When passed to the sum function
15+ // Then it should return 0
16+ test ( "given an empty array, returns 0" , ( ) => {
17+ expect ( sum ( [ ] ) ) . toBe ( 0 ) ;
18+ } ) ;
19+
20+ // Given an array with just one number
21+ // When passed to the sum function
22+ // Then it should return that number
23+ test ( "given an array with one number, returns that number" , ( ) => {
24+ expect ( sum ( [ 15 ] ) ) . toBe ( 15 ) ;
25+ } ) ;
26+
27+ // Given an array containing negative numbers
28+ // When passed to the sum function
29+ // Then it should still return the correct total sum
30+ test ( "given an array with negative numbers, returns the correct sum" , ( ) => {
31+ expect ( sum ( [ 10 , - 6 , 18 , - 10 ] ) ) . toBe ( 12 ) ;
32+ } ) ;
33+
34+ // Given an array with decimal/float numbers
35+ // When passed to the sum function
36+ // Then it should return the correct total sum
37+ test ( "given an array with decimal/float numbers, returns the correct total sum" , ( ) => {
38+ expect ( sum ( [ 65.5 , 8.45 , 32.4 , 97.36 , 10 ] ) ) . toBe ( 213.71 ) ;
39+ } ) ;
40+
41+ // Given an array containing non-number values
42+ // When passed to the sum function
43+ // Then it should ignore the non-numerical values and return the sum of the numerical elements
44+ test ( "given an array with non-number values, ignores them and returns the sum of numerical elements" , ( ) => {
45+ expect ( sum ( [ 10 , "hello" , 25 , null , 5 , undefined , { } , 15 ] ) ) . toBe ( 55 ) ;
46+ } ) ;
47+
48+ // Given an array with only non-number values
49+ // When passed to the sum function
50+ // Then it should return the least surprising value given how it behaves for all other inputs
51+ test ( "given an array with only non-number values, returns 0" , ( ) => {
52+ expect ( sum ( [ "Great!" , "hi" , null , undefined , { } , [ ] ] ) ) . toBe ( 0 ) ;
53+ } ) ;
54+ } ) ;
0 commit comments