Skip to content

Commit de04a51

Browse files
committed
Implement calculateMean with tests
1 parent 1fff256 commit de04a51

2 files changed

Lines changed: 60 additions & 3 deletions

File tree

‎Sprint-1/implement/mean.js‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
1-
function calculateMean(list) {}
1+
function calculateMean(list) {
2+
if (!Array.isArray(list)) {
3+
throw new Error("calculateMean requires an array of numbers");
4+
}
5+
6+
if (list.length === 0) {
7+
throw new Error("calculateMean requires a non-empty array");
8+
}
9+
10+
for (const item of list) {
11+
if (typeof item !== "number") {
12+
throw new Error("calculateMean requires an array of numbers");
13+
}
14+
}
15+
16+
let sum = 0;
17+
for (const num of list) {
18+
sum += num;
19+
}
20+
return sum / list.length;
21+
}
222

323
module.exports = calculateMean;
24+

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

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,29 +22,65 @@ const calculateMean = require("./mean.js");
2222
// Given an array of numbers
2323
// When passed to calculateMean
2424
// Then it should return their mean
25-
// Delete this test.todo and replace it with a test.
26-
test.todo("given [1, 2, 6], returns 3");
25+
26+
test("Given an array of numbers, should return their mean", () => {
27+
expect(calculateMean([3, 7, 50,40])).toEqual(25);
28+
});
2729

2830
// Given an array with a single number
2931
// When passed to calculateMean
3032
// Then it should return that number
33+
test("Given an array with a single number, should return that number", () => {
34+
expect(calculateMean([10])).toEqual(10);
35+
});
3136

3237
// Given an array containing negative or decimal numbers
3338
// When passed to calculateMean
3439
// Then it should return the correct mean
40+
test("Given an array containing negative numbers, should return the correct mean", () => {
41+
expect(calculateMean([-3, -50, -7, -40])).toEqual(-25);
42+
});
43+
44+
test("Given an array containing decimal numbers, should return the correct mean", () => {
45+
expect(calculateMean([2.2, 1.9, 3.5, 4.1])).toEqual(2.925);
46+
});
47+
48+
test("Given an array containing both negative and decimal numbers, should return the correct mean", () => {
49+
expect(calculateMean([2.2, -1.9, 3.5, -4.1])).toEqual(-0.07499999999999984);
50+
});
3551

3652
// Given an empty array
3753
// When passed to calculateMean
3854
// Then it should throw Error("calculateMean requires a non-empty array")
55+
test("Throws an error when given an empty array", () => {
56+
expect(() => calculateMean([])).toThrow(
57+
new Error("calculateMean requires a non-empty array")
58+
);
59+
});
3960

4061
// Given a value that isn't an array, e.g. "banana", 42, null or {}
4162
// When passed to calculateMean
4263
// Then it should throw Error("calculateMean requires an array of numbers")
64+
test("Throws an error when given a value that isn't an array", () => {
65+
expect(() => calculateMean(["banana"])).toThrow(
66+
new Error("calculateMean requires an array of numbers")
67+
);
68+
});
4369

4470
// Given no argument at all
4571
// When passed to calculateMean
4672
// Then it should throw Error("calculateMean requires an array of numbers")
73+
test("Throws an error when given no argument at all", () => {
74+
expect(() => calculateMean()).toThrow(
75+
new Error("calculateMean requires an array of numbers")
76+
);
77+
});
4778

4879
// Given an array containing a non-number value, e.g. [1, "2", 3]
4980
// When passed to calculateMean
5081
// Then it should throw Error("calculateMean requires an array of numbers")
82+
test("Throws an error when given an array containing a non-number value", () => {
83+
expect(() => calculateMean([1, "2", 3])).toThrow(
84+
new Error("calculateMean requires an array of numbers")
85+
);
86+
});

0 commit comments

Comments
 (0)