Skip to content

Commit 2479c53

Browse files
committed
completed test and implementation for mean
1 parent 676c4fa commit 2479c53

3 files changed

Lines changed: 103 additions & 24 deletions

File tree

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,31 +25,31 @@ describe("findMax", () => {
2525
// When passed to the max function
2626
// Then it should return that number
2727
it("returns for [17]", () => {
28-
expect(findMax([17])).toBe(17);
28+
expect(findMax([17])).toEqual(17);
2929
});
3030

3131
// Given an array with both positive and negative numbers
3232
// When passed to the max function
3333
// Then it should return the largest number overall
3434

3535
it("returns for [-6, 15, 30, 42, -90]", () => {
36-
expect(findMax([-6, 15, 30, 42, -90])).toBe(42);
36+
expect(findMax([-6, 15, 30, 42, -90])).toEqual(42);
3737
});
3838

3939
// Given an array with just negative numbers
4040
// When passed to the max function
4141
// Then it should return the closest one to zero
4242

4343
it("returns for [-6, 15, 30, 42, -90]", () => {
44-
expect(findMax([-6, 15, 30, 42, -90])).toBe(42);
44+
expect(findMax([-6, 15, 30, 42, -90])).toEqual(42);
4545
});
4646

4747
// Given an array with decimal numbers
4848
// When passed to the max function
4949
// Then it should return the largest decimal number
5050

5151
it("returns for [0.5, 1.5, 3.5, 5.5, 7.5]", () => {
52-
expect(findMax([0.5, 1.5, 3.5, 5.5, 7.5])).toBe(7.5);
52+
expect(findMax([0.5, 1.5, 3.5, 5.5, 7.5])).toEqual(7.5);
5353
});
5454

5555
// Given an array containing a value that isn't a number

‎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) || 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+
if (list.some((item) => typeof item === "string")) {
11+
throw new Error("calculateMean requires an array of numbers");
12+
}
13+
14+
let sum = 0;
15+
for (const item of list) {
16+
sum += item;
17+
}
18+
19+
const mean = sum / list.length;
20+
21+
return mean;
22+
}
223

324
module.exports = calculateMean;

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

Lines changed: 77 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,28 +23,86 @@ 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+
describe("calculateMean", () => {
27+
it("return [1, 2, 6]", () => {
28+
expect(calculateMean([1, 2, 6])).toEqual(3);
29+
});
2730

28-
// Given an array with a single number
29-
// When passed to calculateMean
30-
// Then it should return that number
31+
// Given an array with a single number
32+
// When passed to calculateMean
33+
// Then it should return that number
34+
it("returns for [6]", () => {
35+
expect(calculateMean([6])).toEqual(6);
36+
});
3137

32-
// Given an array containing negative or decimal numbers
33-
// When passed to calculateMean
34-
// Then it should return the correct mean
38+
// Given an array containing negative or decimal numbers
39+
// When passed to calculateMean
40+
// Then it should return the correct mean
41+
it("returns for 10, 20.5, -10, 30.5]", () => {
42+
expect(calculateMean([10, 20.5, -10, 30.5])).toEqual(12.75);
43+
});
3544

36-
// Given an empty array
37-
// When passed to calculateMean
38-
// Then it should throw Error("calculateMean requires a non-empty array")
45+
// Given an empty array
46+
// When passed to calculateMean
47+
// Then it should throw Error("calculateMean requires a non-empty array")
3948

40-
// Given a value that isn't an array, e.g. "banana", 42, null or {}
41-
// When passed to calculateMean
42-
// Then it should throw Error("calculateMean requires an array of numbers")
49+
it("returns for []", () => {
50+
() =>
51+
expect(calculateMean([])).toThrow(
52+
new Error("calculateMean requires a non-empty array")
53+
);
54+
});
4355

44-
// Given no argument at all
45-
// When passed to calculateMean
46-
// Then it should throw Error("calculateMean requires an array of numbers")
56+
// Given a value that isn't an array, e.g. "banana", 42, null or {}
57+
// When passed to calculateMean
58+
// Then it should throw Error("calculateMean requires an array of numbers")
4759

48-
// Given an array containing a non-number value, e.g. [1, "2", 3]
49-
// When passed to calculateMean
50-
// Then it should throw Error("calculateMean requires an array of numbers")
60+
it("returns for 'banana'", () => {
61+
() =>
62+
expect(calculateMean("banana")).toThrow(
63+
new Error("calculateMean requires an array of numbers")
64+
);
65+
});
66+
67+
it("returns for 42", () => {
68+
() =>
69+
expect(calculateMean(42)).toThrow(
70+
new Error("calculateMean requires an array of numbers")
71+
);
72+
});
73+
74+
it("returns for null", () => {
75+
() =>
76+
expect(calculateMean(null)).toThrow(
77+
new Error("calculateMean requires an array of numbers")
78+
);
79+
});
80+
81+
it("returns for {}", () => {
82+
() =>
83+
expect(calculateMean({})).toThrow(
84+
new Error("calculateMean requires an array of numbers")
85+
);
86+
});
87+
88+
// Given no argument at all
89+
// When passed to calculateMean
90+
// Then it should throw Error("calculateMean requires an array of numbers")
91+
92+
it("returns for ''", () => {
93+
() =>
94+
expect(calculateMean("")).toThrow(
95+
new Error("calculateMean requires an array of numbers")
96+
);
97+
});
98+
99+
// Given an array containing a non-number value, e.g. [1, "2", 3]
100+
// When passed to calculateMean
101+
// Then it should throw Error("calculateMean requires an array of numbers")
102+
it("returns for [1, '2', 3]", () => {
103+
() =>
104+
expect(calculateMean([1, "2", 3])).toThrow(
105+
new Error("calculateMean requires an array of numbers")
106+
);
107+
});
108+
});

0 commit comments

Comments
 (0)