Skip to content

Commit 2cc519c

Browse files
committed
Added tests and implemented solution for the implement section - max.js and max.test.js
1 parent 550590f commit 2cc519c

File tree

2 files changed

+64
-29
lines changed

2 files changed

+64
-29
lines changed

Sprint-1/implement/max.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
11
function findMax(elements) {
2+
if (elements.length === 0) {
3+
return -Infinity;
4+
} else if (elements.length === 1 && typeof elements[0] === "number") {
5+
return elements[0];
6+
}
7+
const maxEl = Math.max(
8+
...elements.filter((el) => typeof el === "number" && !isNaN(el))
9+
);
10+
11+
if (elements.length > 1) {
12+
return maxEl;
13+
}
214
}
315

416
module.exports = findMax;

Sprint-1/implement/max.test.js

Lines changed: 52 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,55 @@ We have set things up already so that this file can see your function from the o
1212

1313
const findMax = require("./max.js");
1414

15-
// Given an empty array
16-
// When passed to the max function
17-
// 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");
20-
21-
// Given an array with one number
22-
// When passed to the max function
23-
// Then it should return that number
24-
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
28-
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
32-
33-
// Given an array with decimal numbers
34-
// When passed to the max function
35-
// Then it should return the largest decimal number
36-
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
40-
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
15+
describe("findMax", () => {
16+
// Given an empty array
17+
// When passed to the max function
18+
// Then it should return -Infinity
19+
test("given an empty array, returns -Infinity", () => {
20+
expect(findMax([])).toBe(-Infinity);
21+
});
22+
23+
// Given an array with one number
24+
// When passed to the max function
25+
// Then it should return that number
26+
test("given an array with one number, returns that number", () => {
27+
expect(findMax([15])).toBe(15);
28+
});
29+
30+
// Given an array with both positive and negative numbers
31+
// When passed to the max function
32+
// Then it should return the largest number overall
33+
test("given an array with both positive and negative numbers, returns the largest number", () => {
34+
expect(findMax([-112, 7, 3, 20, -2])).toBe(20);
35+
});
36+
37+
// Given an array with just negative numbers
38+
// When passed to the max function
39+
// Then it should return the closest one to zero
40+
test("given an array with just negative numbers, returns the largest number - the closest one to zero", () => {
41+
expect(findMax([-653, -80, -7, -4, -12])).toBe(-4);
42+
});
43+
44+
// Given an array with decimal numbers
45+
// When passed to the max function
46+
// Then it should return the largest decimal number
47+
test("given an array with decimal numbers, returns the largest decimal number", () => {
48+
expect(findMax([27.3, 654.1, 45.4, 325.9])).toBe(654.1);
49+
});
50+
51+
// Given an array with non-number values
52+
// When passed to the max function
53+
// Then it should return the max and ignore non-numeric values
54+
test("given an array with non-number values, returns the max and ignores non-numeric values", () => {
55+
expect(
56+
findMax([7, {}, "Greetings!", 34, null, 15.3, undefined, 30, 3878, NaN])
57+
).toBe(3878);
58+
});
59+
60+
// Given an array with only non-number values
61+
// When passed to the max function
62+
// Then it should return the least surprising value given how it behaves for all other inputs
63+
test("given an array with only non-number values, returns -Infinity", () => {
64+
expect(findMax([{}, "Hello!", null, undefined, NaN])).toBe(-Infinity);
65+
});
66+
});

0 commit comments

Comments
 (0)