Skip to content

Commit 45812d4

Browse files
committed
sumtest exercise completed.
1 parent d128741 commit 45812d4

2 files changed

Lines changed: 56 additions & 1 deletion

File tree

Sprint-1/implement/sum.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
function sum(elements) {
2+
let total = 0;
3+
4+
for (let i = 0; i < elements.length; i++) {
5+
if (typeof elements[i] === "number") {
6+
total += elements[i];
7+
}
8+
}
9+
return total;
210
}
311

412
module.exports = sum;

Sprint-1/implement/sum.test.js

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,71 @@ const sum = require("./sum.js");
1313
// Given an empty array
1414
// When passed to the sum function
1515
// Then it should return 0
16-
test.todo("given an empty array, returns 0")
16+
test("given an empty array, returns 0", () => {
17+
const input = [];
18+
const expectedOutput = 0;
19+
expect(sum(input)).toBe(expectedOutput);
20+
});
1721

1822
// Given an array with just one number
1923
// When passed to the sum function
2024
// Then it should return that number
25+
test("given an array with just one number, returns that number", () => {
26+
const input = [5];
27+
const expectedOutput = 5;
28+
expect(sum(input)).toBe(expectedOutput);
29+
});
2130

2231
// Given an array containing negative numbers
2332
// When passed to the sum function
2433
// Then it should still return the correct total sum
34+
test("given an array containing negative numbers, it returns the correct total sum", () => {
35+
const input = [10, -5, 20, -3];
36+
const expectedOutput = 22;
37+
expect(sum(input)).toBe(expectedOutput);
38+
});
2539

2640
// Given an array with decimal/float numbers
2741
// When passed to the sum function
2842
// Then it should return the correct total sum
43+
test("given an array with decimal numbers, it returns the correct total sum", () => {
44+
const input = [1.5, 2.3, 0.7];
45+
const expectedOutput = 4.5;
46+
expect(sum(input)).toBe(expectedOutput);
47+
});
48+
49+
// Given an array with decimal/float numbers
50+
// When passed to the sum function
51+
// Then it should return the correct total sum
52+
test("given an array with decimal numbers, it returns the correct total sum", () => {
53+
const input = [1.5, 2.3, 0.7];
54+
const expectedOutput = 4.5;
55+
expect(sum(input)).toBe(expectedOutput);
56+
});
57+
58+
// Given an array containing non-number values
59+
// When passed to the sum function
60+
// Then it should ignore the non-numerical values and return the sum of the numerical elements
61+
test("given an array containing non-number values, it ignores them and returns the sum of the numerical elements", () => {
62+
const input = [10, "hello", 20, null, 5];
63+
const expectedOutput = 35;
64+
expect(sum(input)).toBe(expectedOutput);
65+
});
2966

3067
// Given an array containing non-number values
3168
// When passed to the sum function
3269
// Then it should ignore the non-numerical values and return the sum of the numerical elements
70+
test("given an array containing non-number values, it ignores them and returns the sum of the numerical elements", () => {
71+
const input = ["hello", "world"];
72+
const expectedOutput = 0;
73+
expect(sum(input)).toBe(expectedOutput);
74+
});
3375

3476
// Given an array with only non-number values
3577
// When passed to the sum function
3678
// Then it should return the least surprising value given how it behaves for all other inputs
79+
test("given an array with only non-number values, it returns the least surprising value", () => {
80+
const input = ["hello", "world"];
81+
const expectedOutput = 0;
82+
expect(sum(input)).toBe(expectedOutput);
83+
});

0 commit comments

Comments
 (0)