|
7 | 7 | const calculateMedian = require("./median.js"); |
8 | 8 |
|
9 | 9 | describe("calculateMedian", () => { |
10 | | - [ |
11 | | - { input: [1, 2, 3], expected: 2 }, |
12 | | - { input: [1, 2, 3, 4, 5], expected: 3 }, |
13 | | - { input: [1, 2, 3, 4], expected: 2.5 }, |
14 | | - { input: [1, 2, 3, 4, 5, 6], expected: 3.5 }, |
15 | | - ].forEach(({ input, expected }) => |
16 | | - it(`returns the median for [${input}]`, () => expect(calculateMedian(input)).toEqual(expected)) |
17 | | - ); |
18 | | - |
19 | | - [ |
20 | | - { input: [3, 1, 2], expected: 2 }, |
21 | | - { input: [5, 1, 3, 4, 2], expected: 3 }, |
22 | | - { input: [4, 2, 1, 3], expected: 2.5 }, |
23 | | - { input: [6, 1, 5, 3, 2, 4], expected: 3.5 }, |
24 | | - { input: [110, 20, 0], expected: 20 }, |
25 | | - { input: [6, -2, 2, 12, 14], expected: 6 }, |
26 | | - ].forEach(({ input, expected }) => |
27 | | - it(`returns the correct median for unsorted array [${input}]`, () => expect(calculateMedian(input)).toEqual(expected)) |
28 | | - ); |
| 10 | + it("returns the median for [1, 2, 3]", () => { |
| 11 | + expect(calculateMedian([1, 2, 3])).toEqual(2); |
| 12 | + }); |
| 13 | + |
| 14 | + it("returns the median for [1, 2, 3, 4, 5]", () => { |
| 15 | + expect(calculateMedian([1, 2, 3, 4, 5])).toEqual(3); |
| 16 | + }); |
| 17 | + |
| 18 | + it("returns the median for [1, 2, 3, 4]", () => { |
| 19 | + expect(calculateMedian([1, 2, 3, 4])).toEqual(2.5); |
| 20 | + }); |
| 21 | + |
| 22 | + it("returns the median for [1, 2, 3, 4, 5, 6]", () => { |
| 23 | + expect(calculateMedian([1, 2, 3, 4, 5, 6])).toEqual(3.5); |
| 24 | + }); |
| 25 | + |
| 26 | + it("returns the correct median for unsorted array [3, 1, 2]", () => { |
| 27 | + expect(calculateMedian([3, 1, 2])).toEqual(2); |
| 28 | + }); |
| 29 | + |
| 30 | + it("returns the correct median for unsorted array [5, 1, 3, 4, 2]", () => { |
| 31 | + expect(calculateMedian([5, 1, 3, 4, 2])).toEqual(3); |
| 32 | + }); |
| 33 | + |
| 34 | + it("returns the correct median for unsorted array [4, 2, 1, 3]", () => { |
| 35 | + expect(calculateMedian([4, 2, 1, 3])).toEqual(2.5); |
| 36 | + }); |
| 37 | + |
| 38 | + it("returns the correct median for unsorted array [6, 1, 5, 3, 2, 4]", () => { |
| 39 | + expect(calculateMedian([6, 1, 5, 3, 2, 4])).toEqual(3.5); |
| 40 | + }); |
| 41 | + |
| 42 | + it("returns the correct median for unsorted array [110, 20, 0]", () => { |
| 43 | + expect(calculateMedian([110, 20, 0])).toEqual(20); |
| 44 | + }); |
| 45 | + |
| 46 | + it("returns the correct median for unsorted array [6, -2, 2, 12, 14]", () => { |
| 47 | + expect(calculateMedian([6, -2, 2, 12, 14])).toEqual(6); |
| 48 | + }); |
29 | 49 |
|
30 | 50 | it("doesn't modify the input array [3, 1, 2]", () => { |
31 | 51 | const list = [3, 1, 2]; |
32 | 52 | calculateMedian(list); |
33 | 53 | expect(list).toEqual([3, 1, 2]); |
34 | 54 | }); |
35 | 55 |
|
36 | | - [ 'not an array', 123, null, undefined, {}, [], ["apple", null, undefined] ].forEach(val => |
37 | | - it(`returns null for non-numeric array (${val})`, () => expect(calculateMedian(val)).toBe(null)) |
38 | | - ); |
39 | | - |
40 | | - [ |
41 | | - { input: [1, 2, "3", null, undefined, 4], expected: 2 }, |
42 | | - { input: ["apple", 1, 2, 3, "banana", 4], expected: 2.5 }, |
43 | | - { input: [1, "2", 3, "4", 5], expected: 3 }, |
44 | | - { input: [1, "apple", 2, null, 3, undefined, 4], expected: 2.5 }, |
45 | | - { input: [3, "apple", 1, null, 2, undefined, 4], expected: 2.5 }, |
46 | | - { input: ["banana", 5, 3, "apple", 1, 4, 2], expected: 3 }, |
47 | | - ].forEach(({ input, expected }) => |
48 | | - it(`filters out non-numeric values and calculates the median for [${input}]`, () => expect(calculateMedian(input)).toEqual(expected)) |
49 | | - ); |
| 56 | + // There is no median of an empty array, so calculateMedian should throw |
| 57 | + it("throws when given an empty array", () => { |
| 58 | + expect(() => calculateMedian([])).toThrow( |
| 59 | + new Error("calculateMedian requires a non-empty array") |
| 60 | + ); |
| 61 | + }); |
| 62 | + |
| 63 | + // Input that isn't an array should throw |
| 64 | + it("throws when given a string", () => { |
| 65 | + expect(() => calculateMedian("banana")).toThrow( |
| 66 | + new Error("calculateMedian requires an array of numbers") |
| 67 | + ); |
| 68 | + }); |
| 69 | + |
| 70 | + it("throws when given a number", () => { |
| 71 | + expect(() => calculateMedian(123)).toThrow( |
| 72 | + new Error("calculateMedian requires an array of numbers") |
| 73 | + ); |
| 74 | + }); |
| 75 | + |
| 76 | + it("throws when given null", () => { |
| 77 | + expect(() => calculateMedian(null)).toThrow( |
| 78 | + new Error("calculateMedian requires an array of numbers") |
| 79 | + ); |
| 80 | + }); |
| 81 | + |
| 82 | + it("throws when given an object", () => { |
| 83 | + expect(() => calculateMedian({})).toThrow( |
| 84 | + new Error("calculateMedian requires an array of numbers") |
| 85 | + ); |
| 86 | + }); |
| 87 | + |
| 88 | + it("throws when called with no argument", () => { |
| 89 | + expect(() => calculateMedian()).toThrow( |
| 90 | + new Error("calculateMedian requires an array of numbers") |
| 91 | + ); |
| 92 | + }); |
| 93 | + |
| 94 | + // Arrays containing any non-number value should throw, rather than filtering them out |
| 95 | + it("throws for an array of strings", () => { |
| 96 | + expect(() => calculateMedian(["ten", "twenty", "thirty"])).toThrow( |
| 97 | + new Error("calculateMedian requires an array of numbers") |
| 98 | + ); |
| 99 | + }); |
| 100 | + |
| 101 | + it("throws for an array mixing numbers and strings", () => { |
| 102 | + expect(() => calculateMedian([1, "2", 3, "4", 5])).toThrow( |
| 103 | + new Error("calculateMedian requires an array of numbers") |
| 104 | + ); |
| 105 | + }); |
| 106 | + |
| 107 | + it("throws for an array containing null", () => { |
| 108 | + expect(() => calculateMedian([1, 2, null, 4])).toThrow( |
| 109 | + new Error("calculateMedian requires an array of numbers") |
| 110 | + ); |
| 111 | + }); |
| 112 | + |
| 113 | + it("throws for an array containing undefined", () => { |
| 114 | + expect(() => calculateMedian([3, 1, undefined, 2])).toThrow( |
| 115 | + new Error("calculateMedian requires an array of numbers") |
| 116 | + ); |
| 117 | + }); |
50 | 118 | }); |
0 commit comments