Skip to content

Commit 553bc80

Browse files
committed
Implement findMax with tests; fix calculateMedian
1 parent 6dcfc89 commit 553bc80

2 files changed

Lines changed: 59 additions & 3 deletions

File tree

‎Sprint-1/implement/max.js‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,26 @@
1+
12
function findMax(elements) {
3+
if (!Array.isArray(elements)) {
4+
throw new Error("findMax requires an array of numbers");
5+
}
6+
7+
for (const item of elements) {
8+
if (typeof item !== "number") {
9+
throw new Error("findMax requires an array of numbers");
10+
}
11+
}
12+
13+
if (elements.length === 0) {
14+
return -Infinity;
15+
}
16+
17+
let max = elements[0];
18+
for (const item of elements) {
19+
if (item > max) {
20+
max = item;
21+
}
22+
}
23+
return max;
224
}
325

426
module.exports = findMax;

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

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,63 @@ const findMax = require("./max.js");
1515
// Given an empty array
1616
// When passed to the max function
1717
// Then it should return -Infinity
18-
// Delete this test.todo and replace it with a test.
19-
test.todo("given an empty array, returns -Infinity");
18+
test("given an empty array, returns -Infinity", () => {
19+
expect(findMax([])).toEqual(-Infinity);
20+
});
2021

2122
// Given an array with one number
2223
// When passed to the max function
2324
// Then it should return that number
25+
test("Given an array with one number, should return thant number", () => {
26+
expect(findMax([10])).toEqual(10);
27+
});
2428

2529
// Given an array with both positive and negative numbers
2630
// When passed to the max function
2731
// Then it should return the largest number overall
32+
test("Given an array with both positive and negative numbers, should return the largest number overall", () => {
33+
expect(findMax([30, -7, 1, 45, -70, 10, -10])).toEqual(45);
34+
});
2835

2936
// Given an array with just negative numbers
3037
// When passed to the max function
3138
// Then it should return the closest one to zero
39+
test("Given an array with just negative numbers, should return the closest one to zero", () => {
40+
expect(findMax([-15, -10, -7, -20, -70])).toEqual(-7);
41+
});
3242

3343
// Given an array with decimal numbers
3444
// When passed to the max function
3545
// Then it should return the largest decimal number
46+
test("Given an array with decimal numbers, should return the largest decimal number", () => {
47+
expect(findMax([3.5, 2.7, 1.9, 5.1])).toEqual(5.1);
48+
});
3649

3750
// Given an array containing a value that isn't a number
3851
// When passed to the max function
3952
// Then it should throw Error("findMax requires an array of numbers")
40-
53+
test("Throws an error when array contains a value that isn't a number", () => {
54+
expect(() => findMax(["plane", 10, "car", 60, 10])).toThrow(
55+
new Error("findMax requires an array of numbers")
56+
);
57+
});
4158
// Given something that isn't an array at all, such as "hey", 42 or no argument
4259
// When passed to the max function
4360
// Then it should throw Error("findMax requires an array of numbers")
61+
test("Throws an error when the input is not an array at all", () => {
62+
expect(() => findMax("hey")).toThrow(
63+
new Error("findMax requires an array of numbers")
64+
);
65+
});
66+
67+
test("Throws an error when given a number", () => {
68+
expect(() => findMax(36)).toThrow(
69+
new Error("findMax requires an array of numbers")
70+
);
71+
});
72+
73+
test("Throws an error when called with no argument", () => {
74+
expect(() => findMax()).toThrow(
75+
new Error("findMax requires an array of numbers")
76+
);
77+
});

0 commit comments

Comments
 (0)