Skip to content

Commit 4213a22

Browse files
committed
implement max function and tests
1 parent 0b63649 commit 4213a22

2 files changed

Lines changed: 23 additions & 19 deletions

File tree

‎Sprint-1/implement/max.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
function findMax(elements) {
2+
const numbers = elements.filter(n => typeof n === "number");
3+
return Math.max(...numbers);
24
}
35

46
module.exports = findMax;

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

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,30 @@ 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("given an empty array, returns -Infinity", () => {
20+
expect(findMax([])).toEqual(-Infinity);
21+
});
2022

21-
// Given an array with one number
22-
// When passed to the max function
23-
// Then it should return that number
23+
test("given an array with one number, returns that number", () => {
24+
expect(findMax([7])).toEqual(7);
25+
});
2426

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
27+
test("given an array with positive and negative numbers, returns the largest", () => {
28+
expect(findMax([-3, 0, 5, -1])).toEqual(5);
29+
});
2830

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
31+
test("given an array with just negative numbers, returns the closest to zero", () => {
32+
expect(findMax([-10, -3, -7])).toEqual(-3);
33+
});
3234

33-
// Given an array with decimal numbers
34-
// When passed to the max function
35-
// Then it should return the largest decimal number
35+
test("given an array with decimal numbers, returns the largest decimal", () => {
36+
expect(findMax([1.1, 2.9, 2.1])).toEqual(2.9);
37+
});
3638

37-
// Given an array with non-number values
38-
// When passed to the max function
39-
// Then it should return the max and ignore non-numeric values
39+
test("given an array with non-number values, ignores them and returns the max", () => {
40+
expect(findMax(["hey", 10, "hi", 60, 10])).toEqual(60);
41+
});
4042

41-
// Given an array with only non-number values
42-
// When passed to the max function
43-
// Then it should return the least surprising value given how it behaves for all other inputs
43+
test("given an array with only non-number values, returns -Infinity", () => {
44+
expect(findMax(["a", "b"])).toEqual(-Infinity);
45+
});

0 commit comments

Comments
 (0)