Skip to content

Commit 0cdf000

Browse files
committed
mean.js completed
1 parent 552c695 commit 0cdf000

2 files changed

Lines changed: 62 additions & 2 deletions

File tree

‎Sprint-1/implement/mean.js‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,26 @@
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+
for (const item of list) {
6+
if (typeof item !== "number") {
7+
throw new Error("calculateMean requires an array of numbers");
8+
}
9+
}
10+
if (list.length === 0) {
11+
throw new Error("calculateMean requires a non-empty array");
12+
}
13+
if (list.length === 1) {
14+
return list[0];
15+
}
16+
17+
let result = 0;
18+
let totalItem = 0;
19+
for (const item of list) {
20+
totalItem += item;
21+
result = totalItem / list.length;
22+
}
23+
return result;
24+
}
225

326
module.exports = calculateMean;

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

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,28 +23,65 @@ const calculateMean = require("./mean.js");
2323
// When passed to calculateMean
2424
// Then it should return their mean
2525
// Delete this test.todo and replace it with a test.
26-
test.todo("given [1, 2, 6], returns 3");
26+
test("given [1, 2, 6], returns 3", () => {
27+
expect(calculateMean([1, 2, 6])).toEqual(3);
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([54])).toEqual(54);
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 or decimal numbers, should return the correct mean", () => {
41+
expect(calculateMean([-3, 3.6, -10, 2.2])).toEqual(-1.8);
42+
});
3543

3644
// Given an empty array
3745
// When passed to calculateMean
3846
// Then it should throw Error("calculateMean requires a non-empty array")
47+
test("given an empty array,should throw Error(calculateMean requires a non-empty array)", () => {
48+
expect(() => calculateMean([])).toThrow(
49+
"calculateMean requires a non-empty array"
50+
);
51+
});
3952

4053
// Given a value that isn't an array, e.g. "banana", 42, null or {}
4154
// When passed to calculateMean
4255
// Then it should throw Error("calculateMean requires an array of numbers")
56+
test("given a value that is not an array,should throw new Error(calculateMean requires an array of numbers)", () => {
57+
expect(() => calculateMean("banana")).toThrow(
58+
"calculateMean requires an array of numbers"
59+
);
60+
expect(() => calculateMean(42)).toThrow(
61+
"calculateMean requires an array of numbers"
62+
);
63+
expect(() => calculateMean(null)).toThrow(
64+
"calculateMean requires an array of numbers"
65+
);
66+
expect(() => calculateMean({})).toThrow(
67+
"calculateMean requires an array of numbers"
68+
);
69+
});
4370

4471
// Given no argument at all
4572
// When passed to calculateMean
4673
// Then it should throw Error("calculateMean requires an array of numbers")
74+
test("given no argument at all, should throw Error(calculateMean requires an array of numbers)", () => {
75+
expect(() => calculateMean()).toThrow(
76+
"calculateMean requires an array of numbers"
77+
);
78+
});
4779

4880
// Given an array containing a non-number value, e.g. [1, "2", 3]
4981
// When passed to calculateMean
5082
// Then it should throw Error("calculateMean requires an array of numbers")
83+
test("given an array containing a non-number value, should throw Error(calculateMean requires an array of numbers)", () => {
84+
expect(() => calculateMean([1, 5, "13"])).toThrow(
85+
"calculateMean requires an array of numbers"
86+
);
87+
});

0 commit comments

Comments
 (0)