Skip to content

Commit 4fb2396

Browse files
committed
sum function created & test cases created
1 parent 64e7ec0 commit 4fb2396

2 files changed

Lines changed: 51 additions & 34 deletions

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+
const numbers = elements.filter((el) => typeof el === "number");
3+
4+
if (numbers.length === 0) return 0;
5+
6+
if (numbers.length === 1) return numbers[0];
7+
8+
const sumNumbers = numbers.reduce((a, b) => a + b, 0);
9+
return sumNumbers;
210
}
311

412
module.exports = sum;

Sprint-1/implement/sum.test.js

Lines changed: 43 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,45 @@
1-
/* Sum the numbers in an array
2-
3-
In this kata, you will need to implement a function that sums the numerical elements of an array
4-
5-
E.g. sum([10, 20, 30]), target output: 60
6-
E.g. sum(['hey', 10, 'hi', 60, 10]), target output: 80 (ignore any non-numerical elements)
7-
*/
8-
91
const sum = require("./sum.js");
102

11-
// 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
3+
describe("sum", () => {
4+
// Given an empty array
5+
// When passed to the sum function
6+
// Then it should return 0
7+
test("given an empty array, returns 0", () => {
8+
expect(sum([])).toBe(0);
9+
});
10+
11+
// Given an array with just one number
12+
// When passed to the sum function
13+
// Then it should return that number
14+
test("given an array with one number, returns that number", () => {
15+
expect(sum([5])).toBe(5);
16+
});
17+
18+
// Given an array containing negative numbers
19+
// When passed to the sum function
20+
// Then it should still return the correct total sum
21+
test("sums negative numbers correctly", () => {
22+
expect(sum([-5, -10, 15])).toBe(0);
23+
});
24+
25+
// Given an array with decimal/float numbers
26+
// When passed to the sum function
27+
// Then it should return the correct total sum
28+
test("sums decimal numbers correctly", () => {
29+
expect(sum([1.5, 2.5, 3.5])).toBe(7.5);
30+
});
31+
32+
// Given an array containing non-number values
33+
// When passed to the sum function
34+
// Then it should ignore the non-numerical values
35+
test("ignores non-number values", () => {
36+
expect(sum(["hey", 10, "hi", 60, 10])).toBe(80);
37+
});
38+
39+
// Given an array with only non-number values
40+
// When passed to the sum function
41+
// Then it should return the least surprising value
42+
test("returns 0 when array contains only non-number values", () => {
43+
expect(sum(["a", "b", "c"])).toBe(0);
44+
});
45+
});

0 commit comments

Comments
 (0)