|
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 | 1 |
|
13 | 2 | const findMax = require("./max.js"); |
14 | 3 |
|
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 |
| 4 | +describe("findMax()", () => { |
| 5 | + // Requirement 1 |
| 6 | + test("finds the largest number in an array", () => { |
| 7 | + expect(findMax([30, 50, 10, 40])).toBe(50); |
| 8 | + }); |
| 9 | + |
| 10 | + // Requirement 2 |
| 11 | + test("given an empty array, returns -Infinity", () => { |
| 12 | + expect(findMax([])).toBe(-Infinity); |
| 13 | + }); |
| 14 | + |
| 15 | + // Requirement 3 |
| 16 | + test("given an array with one number, returns that number", () => { |
| 17 | + expect(findMax([42])).toBe(42); |
| 18 | + }); |
| 19 | + |
| 20 | + // Requirement 4 |
| 21 | + test("finds the largest number in a mix of positive and negative numbers", () => { |
| 22 | + expect(findMax([-10, 5, 0, -2, 20])).toBe(20); |
| 23 | + }); |
| 24 | + |
| 25 | + // Requirement 5 |
| 26 | + test("given only negative numbers, returns the one closest to zero", () => { |
| 27 | + expect(findMax([-50, -10, -40])).toBe(-10); |
| 28 | + }); |
| 29 | + |
| 30 | + // Requirement 6 |
| 31 | + test("finds the largest among decimal numbers", () => { |
| 32 | + expect(findMax([1.5, 1.55, 1.05])).toBe(1.55); |
| 33 | + }); |
| 34 | + |
| 35 | + // Requirement 7 |
| 36 | + test("ignores non-numeric values and finds the max", () => { |
| 37 | + expect(findMax(['hey', 10, 'hi', 60, '10'])).toBe(60); |
| 38 | + }); |
| 39 | + |
| 40 | + // Requirement 8 |
| 41 | + test("given only non-number values, returns -Infinity", () => { |
| 42 | + expect(findMax(['apple', 'orange', ' '])).toBe(-Infinity); |
| 43 | + }); |
| 44 | + |
| 45 | + test("treats numeric strings as numbers", () => { |
| 46 | + expect(findMax(["10", "20", "5"])).toBe(20); |
| 47 | + }); |
| 48 | +}); |
36 | 49 |
|
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 | 50 |
|
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 |
|
0 commit comments