-
-
Notifications
You must be signed in to change notification settings - Fork 278
Expand file tree
/
Copy pathmax.test.js
More file actions
74 lines (64 loc) · 3.18 KB
/
max.test.js
File metadata and controls
74 lines (64 loc) · 3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/* Find the maximum element of an array of numbers
In this kata, you will need to implement a function that find the largest numerical element of an array.
E.g. max([30, 50, 10, 40]), target output: 50
E.g. max(['hey', 10, 'hi', 60, 10]), target output: 60 (sum ignores any non-numerical elements)
You should implement this function in max.js, and add tests for it in this file.
We have set things up already so that this file can see your function from the other file.
*/
const findMax = require("./max.js");
// Given an empty array
// When passed to the max function
// Then it should return -Infinity
// Delete this test.todo and replace it with a test.
test("the function should return -Infinity when an empty array is passed to the function", () => {
expect(findMax([])).toEqual(-Infinity);
});
// Given an array with one number
// When passed to the max function
// Then it should return that number
test("The function should return the original value when an array with one number is passed", () => {
expect(findMax([17])).toBe(17);
expect(findMax([8])).toBe(8);
expect(findMax([4])).toBe(4);
});
// Given an array with both positive and negative numbers
// When passed to the max function
// Then it should return the largest number overall
test("The function should return the max value when an Integers number array is passed", () => {
expect(findMax([-3, -1, 0, 1, 3, 4, 6, 7])).toBe(7);
expect(findMax([-17, -8, -5, 2, 3, 0, 1])).toBe(3);
expect(findMax([-27, 18, -16, -9, 1, 7, 9])).toBe(18);
});
// Given an array with just negative numbers
// When passed to the max function
// Then it should return the closest one to zero
test("The function should return the closest one to zero when an negative number array is passed", () => {
expect(findMax([-3, -1, -4, -6, -7])).toBe(-1);
expect(findMax([-17, -8, -5, -2, -3])).toBe(-2);
expect(findMax([-27, -18, -32, -67, -78, -70])).toBe(-18);
});
// Given an array with decimal numbers
// When passed to the max function
// Then it should return the largest decimal number
test("The function should return the closest one to zero when an negative number array is passed", () => {
expect(findMax([3.5, 1.9, 5.4, 0.6, 3.7])).toBe(5.4);
expect(findMax([1.7, 0.8, 2.532, 1.092, 0.3])).toBe(2.532);
expect(findMax([0.27, 0.18, 0.32, 0.67, 0.78, 0.7])).toBe(0.78);
});
// Given an array with non-number values
// When passed to the max function
// Then it should return the max and ignore non-numeric values
test("The function should return the closest one to zero when an negative number array is passed", () => {
expect(findMax([3.5, "Leeds", "300", "London", 3.7])).toBe(3.7);
expect(findMax(["1.778", "italy", 0.8, 0.88, 0.89, "base"])).toBe(0.89);
expect(findMax([0.27, "jerry", "140", 0.32, "apple", 0.78, "fly"])).toBe(
0.78
);
});
// Given an array with only non-number values
// When passed to the max function
// Then it should return the least surprising value given how it behaves for all other inputs
test("the function should return undefined when non-number value is passed", () => {
expect(findMax(["Manchester", "4000", "Leeds", "London"])).toBe(undefined);
expect(findMax(["orange", "apple", "5", "banana"])).toBe(undefined);
});