Skip to content

Commit 8e2d553

Browse files
committed
sum.js completed
1 parent 5e63389 commit 8e2d553

2 files changed

Lines changed: 42 additions & 1 deletion

File tree

‎Sprint-1/implement/sum.js‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,21 @@
11
function sum(elements) {
2+
if (!Array.isArray(elements)) {
3+
throw new Error("sum requires an array of numbers");
4+
}
5+
if (elements.includes(NaN)) {
6+
throw new Error("sum requires an array of numbers");
7+
}
8+
if (elements.length === 0) {
9+
return 0;
10+
}
11+
if (elements.length === 1) {
12+
return elements[0];
13+
}
14+
let total = 0;
15+
for (const item of elements) {
16+
total += item;
17+
}
18+
return total;
219
}
320

421
module.exports = sum;

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,48 @@ 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+
expect(sum([])).toEqual(0);
18+
});
1719

1820
// Given an array with just one number
1921
// When passed to the sum function
2022
// Then it should return that number
23+
test("given an array with just one number, should return that number", () => {
24+
expect(sum([1])).toEqual(1);
25+
});
2126

2227
// Given an array containing negative numbers
2328
// When passed to the sum function
2429
// Then it should still return the correct total sum
30+
test("given an array containing negative numbers,should return the correct total sum", () => {
31+
expect(sum([-2, -4, -1])).toEqual(-7);
32+
});
2533

2634
// Given an array with decimal/float numbers
2735
// When passed to the sum function
2836
// Then it should return the correct total sum
37+
test("given an array wit decimal numbers, should return correct total sum", () => {
38+
expect(sum([3.1, 2.1, 8.8])).toEqual(14);
39+
});
2940

3041
// Given an array containing a value that isn't a number
3142
// When passed to the sum function
3243
// Then it should throw Error("sum requires an array of numbers")
44+
test("given an array containing a value that is not a number, should throw Error(sum requires an array of numbers)", () => {
45+
expect(() => sum([NaN, 21])).toThrow("sum requires an array of numbers");
46+
});
3347

3448
// Given something that isn't an array at all, such as "hey", 42 or no argument
3549
// When passed to the sum function
3650
// Then it should throw Error("sum requires an array of numbers")
51+
test("given something that isn't an array at all, should throw Error(sum requires an array of numbers)", () => {
52+
expect(() => sum(21)).toThrow("sum requires an array of numbers");
53+
});
54+
55+
test("given something that isn't an array at all, should throw Error(sum requires an array of numbers)", () => {
56+
expect(() => sum("hello")).toThrow("sum requires an array of numbers");
57+
});
58+
test("given something that isn't an array at all, should throw Error(sum requires an array of numbers)", () => {
59+
expect(() => sum()).toThrow("sum requires an array of numbers");
60+
});

0 commit comments

Comments
 (0)