Skip to content

Commit bb247fd

Browse files
committed
Implement findMax and add full test coverage for all input scenarios
1 parent 4649963 commit bb247fd

2 files changed

Lines changed: 27 additions & 1 deletion

File tree

‎Sprint-1/implement/max.js‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
function findMax(elements) {
2+
const numbers = elements.filter((item) => typeof item === "number");
3+
if (numbers.length === 0) {
4+
return -Infinity;
5+
}
6+
return Math.max(...numbers);
27
}
38

49
module.exports = findMax;

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,49 @@ 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+
//test.todo("given an empty array, returns -Infinity");
20+
test("given an empty array, returns -Infinity", () => {
21+
expect(findMax([])).toBe(-Infinity);
22+
});
2023

2124
// Given an array with one number
2225
// When passed to the max function
2326
// Then it should return that number
27+
test("given an array with one number, returns that number", () => {
28+
expect(findMax([42])).toBe(42);
29+
});
2430

2531
// Given an array with both positive and negative numbers
2632
// When passed to the max function
2733
// Then it should return the largest number overall
34+
test("given an array with positive and negative numbers, returns the largest", () => {
35+
expect(findMax([-10, 5, 3, -2])).toBe(5);
36+
});
2837

2938
// Given an array with just negative numbers
3039
// When passed to the max function
3140
// Then it should return the closest one to zero
41+
test("given an array with only negative numbers, returns the closest to zero", () => {
42+
expect(findMax([-10, -3, -20])).toBe(-3);
43+
});
3244

3345
// Given an array with decimal numbers
3446
// When passed to the max function
3547
// Then it should return the largest decimal number
48+
test("given an array with decimal numbers, returns the largest decimal", () => {
49+
expect(findMax([1.2, 3.5, 2.8])).toBe(3.5);
50+
});
3651

3752
// Given an array with non-number values
3853
// When passed to the max function
3954
// Then it should return the max and ignore non-numeric values
55+
test("given an array with non-number values, ignores them and returns the max", () => {
56+
expect(findMax(['hey', 10, 'hi', 60, 10])).toBe(60);
57+
});
4058

4159
// Given an array with only non-number values
4260
// When passed to the max function
4361
// Then it should return the least surprising value given how it behaves for all other inputs
62+
test("given an array with only non-number values, returns -Infinity", () => {
63+
expect(findMax(['a', 'b', null, undefined])).toBe(-Infinity);
64+
});

0 commit comments

Comments
 (0)