-
-
Notifications
You must be signed in to change notification settings - Fork 283
Expand file tree
/
Copy pathsum.test.js
More file actions
82 lines (68 loc) · 2.43 KB
/
sum.test.js
File metadata and controls
82 lines (68 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/* Sum the numbers in an array
In this kata, you will need to implement a function that sums the numerical elements of an array
E.g. sum([10, 20, 30]), target output: 60
E.g. sum(['hey', 10, 'hi', 60, 10]), target output: 80 (ignore any non-numerical elements)
*/
const sum = require("./sum.js");
// Acceptance Criteria:
describe("sum()", () => {
// Given an empty array
// When passed to the sum function
// Then it should return 0
[{ input: [], expected: 0 }].forEach(({ input, expected }) =>
it(`should return ${expected} for [${input}]`, () => {
expect(sum(input)).toEqual(expected);
})
);
// Given an array with just one number
// When passed to the sum function
// Then it should return that number
[{ input: [30], expected: 30 }].forEach(({ input, expected }) =>
it(`should return ${expected} for [${input}]`, () => {
expect(sum(input)).toEqual(expected);
})
);
// Given an array containing negative numbers
// When passed to the sum function
// Then it should still return the correct total sum
[{ input: [-1, -3, -4, -11], expected: -19 }].forEach(({ input, expected }) =>
it(`should return ${expected} for [${input}]`, () => {
expect(sum(input)).toEqual(expected);
})
);
// Given an array with decimal/float numbers
// When passed to the sum function
// Then it should return the correct total sum
[{ input: [0.5, 0.2, 0.11, 0.89, 0.3], expected: 2 }].forEach(
({ input, expected }) =>
it(`should return ${expected} for [${input}]`, () => {
expect(sum(input)).toEqual(expected);
})
);
// Given an array containing non-number values
// When passed to the sum function
// Then it should ignore the non-numerical values and return the sum of the numerical elements
[
{
input: ["evan", 3, "mike", 20, 6, "", "/", undefined, null, 20],
expected: 49,
},
].forEach(({ input, expected }) =>
it(`should return ${expected} for [${input}]`, () => {
expect(sum(input)).toEqual(expected);
})
);
// Given an array with only non-number values
// When passed to the sum function
// Then it should return the least surprising value given how it behaves for all other inputs
[
{
input: ["evan", "mike", "",undefined],
expected: "invalid elements",
},
].forEach(({ input, expected }) =>
it(`should return ${expected} for [${input}]`, () => {
expect(sum(input)).toEqual(expected);
})
);
});