Skip to content

Commit 676c4fa

Browse files
committed
completed test and implementation for max.
1 parent e2480de commit 676c4fa

2 files changed

Lines changed: 70 additions & 20 deletions

File tree

‎Sprint-1/implement/max.js‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
function findMax(elements) {
2+
if (!Array.isArray(elements)) {
3+
throw new Error("findMax requires an array of numbers");
4+
}
5+
if (elements.some((element) => typeof element === "string")) {
6+
throw new Error("findMax requires an array of numbers");
7+
}
8+
return Math.max(...elements);
29
}
3-
410
module.exports = findMax;

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

Lines changed: 63 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,72 @@ const findMax = require("./max.js");
1616
// When passed to the max function
1717
// Then it should return -Infinity
1818
// Delete this test.todo and replace it with a test.
19-
test.todo("given an empty array, returns -Infinity");
19+
describe("findMax", () => {
20+
it("returns for []", () => {
21+
expect(findMax([])).toEqual(-Infinity);
22+
});
2023

21-
// Given an array with one number
22-
// When passed to the max function
23-
// Then it should return that number
24+
// Given an array with one number
25+
// When passed to the max function
26+
// Then it should return that number
27+
it("returns for [17]", () => {
28+
expect(findMax([17])).toBe(17);
29+
});
2430

25-
// Given an array with both positive and negative numbers
26-
// When passed to the max function
27-
// Then it should return the largest number overall
31+
// Given an array with both positive and negative numbers
32+
// When passed to the max function
33+
// Then it should return the largest number overall
2834

29-
// Given an array with just negative numbers
30-
// When passed to the max function
31-
// Then it should return the closest one to zero
35+
it("returns for [-6, 15, 30, 42, -90]", () => {
36+
expect(findMax([-6, 15, 30, 42, -90])).toBe(42);
37+
});
3238

33-
// Given an array with decimal numbers
34-
// When passed to the max function
35-
// Then it should return the largest decimal number
39+
// Given an array with just negative numbers
40+
// When passed to the max function
41+
// Then it should return the closest one to zero
3642

37-
// Given an array containing a value that isn't a number
38-
// When passed to the max function
39-
// Then it should throw Error("findMax requires an array of numbers")
43+
it("returns for [-6, 15, 30, 42, -90]", () => {
44+
expect(findMax([-6, 15, 30, 42, -90])).toBe(42);
45+
});
4046

41-
// Given something that isn't an array at all, such as "hey", 42 or no argument
42-
// When passed to the max function
43-
// Then it should throw Error("findMax requires an array of numbers")
47+
// Given an array with decimal numbers
48+
// When passed to the max function
49+
// Then it should return the largest decimal number
50+
51+
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);
53+
});
54+
55+
// Given an array containing a value that isn't a number
56+
// When passed to the max function
57+
// Then it should throw Error("findMax requires an array of numbers")
58+
59+
it("returns for [2, 4, 'hello', 6, 8, 10]", () => {
60+
() =>
61+
expect(findMax([2, 4, "hello", 6, 8, 10])).toThrow(
62+
new Error("findMax requires an array of numbers")
63+
);
64+
});
65+
66+
// Given something that isn't an array at all, such as "hey", 42 or no argument
67+
// When passed to the max function
68+
// Then it should throw Error("findMax requires an array of numbers")
69+
70+
it("returns for 'hey'", () => {
71+
expect(() => findMax("hey")).toThrow(
72+
new Error("findMax requires an array of numbers")
73+
);
74+
});
75+
76+
it("returns for 42", () => {
77+
expect(() => findMax(42)).toThrow(
78+
new Error("findMax requires an array of numbers")
79+
);
80+
});
81+
82+
it("returns for ''", () => {
83+
expect(() => findMax()).toThrow(
84+
new Error("findMax requires an array of numbers")
85+
);
86+
});
87+
});

0 commit comments

Comments
 (0)