Skip to content

Commit 0799426

Browse files
committed
Added tests and implemented solution for the implement section - sum.js and sum.test.js
1 parent 2cc519c commit 0799426

File tree

2 files changed

+58
-25
lines changed

2 files changed

+58
-25
lines changed

Sprint-1/implement/sum.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
11
function sum(elements) {
2+
const numbers = elements.filter(
3+
(item) => typeof item === "number" && !isNaN(item)
4+
);
5+
6+
if (numbers.length === 0) return 0;
7+
8+
const maxDecimalPlaces = numbers.reduce(
9+
(currMax, el) => Math.max(currMax, (String(el).split(".") || "").length),
10+
0
11+
);
12+
const decMultiplier = 10 ** maxDecimalPlaces;
13+
14+
const total = numbers.reduce((acc, el) => acc + el, 0);
15+
16+
return Math.round((total + Number.EPSILON) * decMultiplier) / decMultiplier;
217
}
318

419
module.exports = sum;

Sprint-1/implement/sum.test.js

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,46 @@ E.g. sum(['hey', 10, 'hi', 60, 10]), target output: 80 (ignore any non-numerical
99
const 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

Comments
 (0)