Skip to content

Commit 0bc4a3a

Browse files
committed
completed test and implementation for sum
1 parent 2479c53 commit 0bc4a3a

2 files changed

Lines changed: 62 additions & 16 deletions

File tree

‎Sprint-1/implement/sum.js‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,17 @@
11
function sum(elements) {
2+
if (!Array.isArray(elements) || elements === "") {
3+
throw new Error("sum requires an array of numbers");
4+
}
5+
6+
if (elements.some((element) => typeof element === "string")) {
7+
throw new Error("sum requires an array of numbers");
8+
}
9+
let result = 0;
10+
for (const element of elements) {
11+
result += element;
12+
}
13+
14+
return result;
215
}
316

417
module.exports = sum;

‎Sprint-1/implement/sum.test.js‎

Lines changed: 49 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,57 @@ 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+
describe("sum", () => {
17+
it("returns for []", () => {
18+
expect(sum([])).toEqual(0);
19+
});
20+
// Given an array with just one number
21+
// When passed to the sum function
22+
// Then it should return that number
23+
it("returns for [8]", () => {
24+
expect(sum([8])).toEqual(8);
25+
});
26+
// Given an array containing negative numbers
27+
// When passed to the sum function
28+
// Then it should still return the correct total sum
29+
it("returns for [8, -5, 10]", () => {
30+
expect(sum([8, -5, 10])).toEqual(13);
31+
});
1732

18-
// Given an array with just one number
19-
// When passed to the sum function
20-
// Then it should return that number
33+
// Given an array with decimal/float numbers
34+
// When passed to the sum function
35+
// Then it should return the correct total sum
36+
it("returns for [10, 3.75, 2.25]", () => {
37+
expect(sum([10, 3.75, 2.25])).toEqual(16);
38+
});
39+
// Given an array containing a value that isn't a number
40+
// When passed to the sum function
41+
// Then it should throw Error("sum requires an array of numbers")
2142

22-
// Given an array containing negative numbers
23-
// When passed to the sum function
24-
// Then it should still return the correct total sum
43+
it("returns for [10, 20, '30']", () => {
44+
() =>
45+
expect(
46+
sum([10, 20, "30"]).toThrow(
47+
new Error("sum requires an array of numbers")
48+
)
49+
);
50+
});
2551

26-
// Given an array with decimal/float numbers
27-
// When passed to the sum function
28-
// Then it should return the correct total sum
52+
// Given something that isn't an array at all, such as "hey", 42 or no argument
53+
// When passed to the sum function
54+
// Then it should throw Error("sum requires an array of numbers")
55+
it("returns for 'hey'", () => {
56+
() =>
57+
expect(sum("hey")).toThrow(new Error("sum requires an array of numbers"));
58+
});
2959

30-
// Given an array containing a value that isn't a number
31-
// When passed to the sum function
32-
// Then it should throw Error("sum requires an array of numbers")
60+
it("returns for (42)", () => {
61+
() =>
62+
expect(sum(42)).toThrow(new Error("sum requires an array of numbers"));
63+
});
3364

34-
// Given something that isn't an array at all, such as "hey", 42 or no argument
35-
// When passed to the sum function
36-
// Then it should throw Error("sum requires an array of numbers")
65+
it("returns for ('')", () => {
66+
() =>
67+
expect(sum("")).toThrow(new Error("sum requires an array of numbers"));
68+
});
69+
});

0 commit comments

Comments
 (0)