Skip to content

Commit 64e7ec0

Browse files
committed
findMax function created & test cases created
1 parent 8bac04f commit 64e7ec0

2 files changed

Lines changed: 65 additions & 41 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+
const numbers = elements.filter((el) => typeof el === "number");
3+
4+
if (numbers.length === 0) {
5+
return -Infinity;
6+
}
7+
8+
let max = numbers[0];
9+
10+
for (let i = 1; i < numbers.length; i++) {
11+
if (numbers[i] > max) {
12+
max = numbers[i];
13+
}
14+
}
15+
16+
return max;
217
}
318

419
module.exports = findMax;

Sprint-1/implement/max.test.js

Lines changed: 50 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,52 @@
1-
/* Find the maximum element of an array of numbers
2-
3-
In this kata, you will need to implement a function that find the largest numerical element of an array.
4-
5-
E.g. max([30, 50, 10, 40]), target output: 50
6-
E.g. max(['hey', 10, 'hi', 60, 10]), target output: 60 (sum ignores any non-numerical elements)
7-
8-
You should implement this function in max.js, and add tests for it in this file.
9-
10-
We have set things up already so that this file can see your function from the other file.
11-
*/
12-
131
const findMax = require("./max.js");
142

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
3+
describe("findMax", () => {
4+
// Given an empty array
5+
// When passed to the max function
6+
// Then it should return -Infinity
7+
test("given an empty array, returns -Infinity", () => {
8+
expect(findMax([])).toBe(-Infinity);
9+
});
10+
11+
// Given an array with one number
12+
// When passed to the max function
13+
// Then it should return that number
14+
test("given an array with one number, returns that number", () => {
15+
expect(findMax([5])).toBe(5);
16+
});
17+
18+
// Given an array with both positive and negative numbers
19+
// When passed to the max function
20+
// Then it should return the largest number overall
21+
test("returns the largest number from positive and negative numbers", () => {
22+
expect(findMax([-10, 20, -5, 15])).toBe(20);
23+
});
24+
25+
// Given an array with just negative numbers
26+
// When passed to the max function
27+
// Then it should return the closest one to zero
28+
test("returns the largest number among negative numbers", () => {
29+
expect(findMax([-10, -3, -50, -1])).toBe(-1);
30+
});
31+
32+
// Given an array with decimal numbers
33+
// When passed to the max function
34+
// Then it should return the largest decimal number
35+
test("returns the largest decimal number", () => {
36+
expect(findMax([1.2, 3.7, 2.5, 3.6])).toBe(3.7);
37+
});
38+
39+
// Given an array with non-number values
40+
// When passed to the max function
41+
// Then it should return the max and ignore non-numeric values
42+
test("ignores non-number values", () => {
43+
expect(findMax(["hey", 10, "hi", 60, 10])).toBe(60);
44+
});
45+
46+
// Given an array with only non-number values
47+
// When passed to the max function
48+
// Then it should return the least surprising value given how it behaves for all other inputs
49+
test("returns -Infinity when array has only non-number values", () => {
50+
expect(findMax(["a", "b", "c"])).toBe(-Infinity);
51+
});
52+
});

0 commit comments

Comments
 (0)