Skip to content

Commit 4359456

Browse files
committed
accepted incoming changes for sum.js and sum.test.js
1 parent 19b40af commit 4359456

2 files changed

Lines changed: 36 additions & 45 deletions

File tree

‎Sprint-1/implement/sum.js‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
function sum(elements) {
2-
let total = 0;
1+
function sum(list) {
2+
if (!Array.isArray(list)) {
3+
throw new Error("sum requires an array of numbers");
4+
}
35

4-
for (let i = 0; i < elements.length; i++) {
5-
if (typeof elements[i] === "number" && !isNaN(elements[i])) {
6-
total += elements[i];
7-
}
6+
if (!list.every((item) => typeof item === "number" && !Number.isNaN(item))) {
7+
throw new Error("sum requires an array of numbers");
88
}
99

10-
return total;
10+
return list.reduce((total, number) => total + number, 0);
1111
}
1212

1313
module.exports = sum;

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

Lines changed: 29 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,9 @@
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");
10-
// Acceptance Criteria:
2+
113
// Given an empty array
124
// When passed to the sum function
135
// Then it should return 0
14-
15-
test("returns 0 for empty array", () => {
6+
test("returns 0 for an empty array", () => {
167
const list = [];
178
const currentOutput = sum(list);
189
const targetOutput = 0;
@@ -23,54 +14,54 @@ test("returns 0 for empty array", () => {
2314
// Given an array with just one number
2415
// When passed to the sum function
2516
// Then it should return that number
26-
27-
test("sum of 1 number", () => {
17+
test("returns the number when the array contains one number", () => {
2818
const list = [1];
2919
const currentOutput = sum(list);
30-
3120
const targetOutput = 1;
21+
3222
expect(currentOutput).toEqual(targetOutput);
3323
});
24+
3425
// Given an array containing negative numbers
3526
// When passed to the sum function
3627
// Then it should still return the correct total sum
37-
38-
test("sum of negative number in arrays", () => {
39-
const list = [-5];
28+
test("returns the correct sum with negative numbers", () => {
29+
const list = [-5, -10, 3];
4030
const currentOutput = sum(list);
41-
const targetOutput = -5;
31+
const targetOutput = -12;
32+
4233
expect(currentOutput).toEqual(targetOutput);
4334
});
4435

4536
// Given an array with decimal/float numbers
4637
// When passed to the sum function
4738
// Then it should return the correct total sum
48-
49-
test("sum of decimal/float number in arrays", () => {
50-
const list = [4.5, 2.5];
39+
test("returns the correct sum with decimal numbers", () => {
40+
const list = [2.5, 3.5, 4];
5141
const currentOutput = sum(list);
52-
const targetOutput = 7;
42+
const targetOutput = 10;
43+
5344
expect(currentOutput).toEqual(targetOutput);
5445
});
5546

56-
// Given an array containing non-number values
47+
// Given an array containing a value that isn't a number
5748
// When passed to the sum function
58-
// Then it should ignore the non-numerical values and return the sum of the numerical elements
59-
test("ignores non-number values", () => {
60-
const list = ["hey", 10, "hi", 60, 10];
61-
const currentOutput = sum(list);
62-
const targetOutput = 80;
63-
64-
expect(currentOutput).toEqual(targetOutput);
49+
// Then it should throw Error("sum requires an array of numbers")
50+
test("throws an error when the array contains a non-number", () => {
51+
expect(() => sum(["hey", 10, "hi", 60, 10])).toThrow(
52+
new Error("sum requires an array of numbers")
53+
);
6554
});
6655

67-
test("ignores NaN values when calculating the sum", () => {
68-
const list = [4, NaN, 6];
69-
const currentOutput = sum(list);
70-
const targetOutput = 10;
56+
// Given something that isn't an array at all
57+
// When passed to the sum function
58+
// Then it should throw Error("sum requires an array of numbers")
59+
test("throws an error when the input is not an array", () => {
60+
expect(() => sum("hey")).toThrow(
61+
new Error("sum requires an array of numbers")
62+
);
7163

72-
expect(currentOutput).toEqual(targetOutput);
64+
expect(() => sum(42)).toThrow(new Error("sum requires an array of numbers"));
65+
66+
expect(() => sum()).toThrow(new Error("sum requires an array of numbers"));
7367
});
74-
// Given an array with only non-number values
75-
// When passed to the sum function
76-
// Then it should return the least surprising value given how it behaves for all other inputs

0 commit comments

Comments
 (0)