|
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 | | -// 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"); |
| 3 | +test("Given an array with only +ve numbers and should return the largest of them", () => { |
| 4 | + expect(findMax([2, 4, 2, 1, 5])).toEqual(5); |
| 5 | +}); |
| 6 | + |
| 7 | +test("Given an empty array should return -Infinity", () => { |
| 8 | + expect(findMax([])).toEqual(-Infinity); |
| 9 | +}); |
20 | 10 |
|
21 | | -// Given an array with one number |
22 | | -// When passed to the max function |
23 | | -// Then it should return that number |
| 11 | +test("Given an array with one number only and it should return that number", () => { |
| 12 | + expect(findMax([7])).toEqual(7); |
| 13 | +}); |
24 | 14 |
|
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 |
| 15 | +test("Given an array with both positive and negative numbers and it should return the largest number", () => { |
| 16 | + expect(findMax([1, -3, -2, 3, 4])).toEqual(4); |
| 17 | +}); |
28 | 18 |
|
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 |
| 19 | +test("Given an array with negative numbers and it should return the largest number", () => { |
| 20 | + expect(findMax([-2, -5, -5, -3, -10])).toEqual(-2); |
| 21 | +}); |
32 | 22 |
|
33 | | -// Given an array with decimal numbers |
34 | | -// When passed to the max function |
35 | | -// Then it should return the largest decimal number |
| 23 | +test("Given an array with decimal numbers and it should return the largest decimal numbers", () => { |
| 24 | + expect(findMax([2.7, 2.01, 4.4, 5.2])).toEqual(5.2); |
| 25 | +}); |
36 | 26 |
|
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 |
| 27 | +test("Given an array a mix of non-numeric values and numeric values and it should return the largest from only the numeric values and ignore the non-numeric ones", () => { |
| 28 | + expect(findMax([2, "a", 8, "12", 8.1])).toEqual(8.1); |
| 29 | +}); |
40 | 30 |
|
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 |
| 31 | +test("Given an array with only non-numeric values and it should return -Infinity", () => { |
| 32 | + expect(findMax(["a", "e", "I", "o", "U"])).toEqual(-Infinity); |
| 33 | +}); |
0 commit comments