Skip to content

Commit 5e63389

Browse files
committed
max.js completed
1 parent a5d9c95 commit 5e63389

2 files changed

Lines changed: 48 additions & 3 deletions

File tree

‎Sprint-1/implement/max.js‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
11
function findMax(elements) {
2+
if (!Array.isArray(elements)) {
3+
throw new Error("findMax requires an array of numbers");
4+
}
5+
if (elements.includes(NaN)) {
6+
throw new Error("findMax requires an array of numbers");
7+
}
8+
if (elements.length === 0) {
9+
return -Infinity;
10+
}
11+
12+
if (elements.length === 1) {
13+
return elements[0];
14+
}
15+
const sortedList = elements.toSorted((a, b) => a - b);
16+
return sortedList.at(-1);
217
}
318

419
module.exports = findMax;

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

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,58 @@ 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");
20-
19+
test("given an empty array, returns -Infinity", () => {
20+
expect(findMax([])).toEqual(-Infinity);
21+
});
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, returns that number", () => {
26+
expect(findMax([4])).toEqual(4);
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 returns the largest number overall", () => {
33+
expect(findMax([4, -2])).toEqual(4);
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([-2, -54])).toEqual(-2);
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, 10.8])).toEqual(10.8);
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 an array contains a non-number", () => {
54+
expect(() => findMax([10, NaN, 22])).toThrow(
55+
"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 input is a string", () => {
62+
expect(() => findMax("hello")).toThrow(
63+
"findMax requires an array of numbers"
64+
);
65+
});
66+
67+
test("throws an error when input is a number", () => {
68+
expect(() => findMax(42)).toThrow("findMax requires an array of numbers");
69+
});
70+
71+
test("throws an error when no argument is provided", () => {
72+
expect(() => findMax()).toThrow("findMax requires an array of numbers");
73+
});

0 commit comments

Comments
 (0)