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