Skip to content

Commit 0d3fb03

Browse files
Alex JamshidiAlex Jamshidi
authored andcommitted
added solutions to max.js
1 parent 81f0596 commit 0d3fb03

2 files changed

Lines changed: 45 additions & 2 deletions

File tree

Sprint-1/implement/max.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
function findMax(elements) {
1+
function findMax(list) {
2+
const numbers = list.filter(n => Number.isFinite(n));
3+
if (numbers.length == 0) { return "-Infinity"}
4+
else {return Math.max(...numbers)};
25
}
36

47
module.exports = findMax;

Sprint-1/implement/max.test.js

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,72 @@ 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+
describe("calculateMedian", () => {
1516
// Given an empty array
1617
// When passed to the max function
1718
// Then it should return -Infinity
1819
// Delete this test.todo and replace it with a test.
19-
test.todo("given an empty array, returns -Infinity");
20+
21+
it("empty array returns -Infinity", () => {
22+
const list = [];
23+
expect(findMax(list)).toEqual("-Infinity");
24+
});
2025

2126
// Given an array with one number
2227
// When passed to the max function
2328
// Then it should return that number
2429

30+
it("array with one number returns that number", () => {
31+
const list = [2];
32+
expect(findMax(list)).toEqual(2);
33+
});
34+
2535
// Given an array with both positive and negative numbers
2636
// When passed to the max function
2737
// Then it should return the largest number overall
2838

39+
it("array with +ve and -ve numbers, returns largest number overall", () => {
40+
const list = [10, -15];
41+
expect(findMax(list)).toEqual(10);
42+
});
43+
44+
2945
// Given an array with just negative numbers
3046
// When passed to the max function
3147
// Then it should return the closest one to zero
3248

49+
it("array -ve numbers, returns closest to zero", () => {
50+
const list = [-1, -2, -3, -15];
51+
expect(findMax(list)).toEqual(-1);
52+
});
53+
54+
3355
// Given an array with decimal numbers
3456
// When passed to the max function
3557
// Then it should return the largest decimal number
3658

59+
it("array with decimal numbers should return largest decimal number", () => {
60+
const list = [1.9, 2.8999, 3.1];
61+
expect(findMax(list)).toEqual(3.1);
62+
});
63+
64+
3765
// Given an array with non-number values
3866
// When passed to the max function
3967
// Then it should return the max and ignore non-numeric values
4068

69+
it("array with non-number values, returns the max and ignore non-numeric values", () => {
70+
const list = [1, "a", 2, "b", 3, "c"];
71+
expect(findMax(list)).toEqual(3);
72+
});
73+
74+
4175
// Given an array with only non-number values
4276
// When passed to the max function
4377
// Then it should return the least surprising value given how it behaves for all other inputs
78+
79+
it("array with only non-number values, return the least surprising value given how it behaves for all other inputs", () => {
80+
const list = ["a", "b", "c"];
81+
expect(findMax(list)).toEqual("-Infinity");
82+
});
83+
});

0 commit comments

Comments
 (0)