Skip to content

Commit 7e39c55

Browse files
authored
Update Solution and tests for Sprint 1 'fix median' task (#390)
* Update solution and tests for Sprint 1 'fix' exercise with median * Add tests to check for errors and mixed arrays (advanced implementation of median) * Create two vesions of calculate median function: with throw error and with return null * Remove calculate median with throwing error and related tests
1 parent c0a0113 commit 7e39c55

2 files changed

Lines changed: 56 additions & 19 deletions

File tree

‎Sprint-1/fix/median.js‎

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,33 @@
1111
// }
1212

1313
// Explanation:
14-
// .splice() is a mutating method, so it updates the array in place.
15-
// to prevent this, you could take a copy of the array and use .splice(),
16-
// or you could use bracket notation to element at the desired index
14+
// The original implementation has several mistakes:
15+
// 1 - .splice() mutates or changes the original array. We don't want this to happen.
16+
// 2 - We actually don't need .splice() or any other method to find a median.
17+
// 3 - We must sort the array before we search for the median.
18+
// 4 - We should check if the 'list' parameter actually has numbers, otherwise try to filter
19+
// out all non-numeric values and throw an error or return null if there are no numeric values.
1720

18-
function calculateMedian(list) {
19-
const middleIndex = Math.floor(list.length / 2);
2021

21-
if (list.length % 2 === 0) { // use the remainder operator (%) to check if the array length is even
22-
return (list[middleIndex - 1] + list[middleIndex]) / 2;
22+
// Example of a function calculating median and returning null in case no numeric values provided.
23+
24+
function calculateMedian(list) {
25+
if (!Array.isArray(list)) {
26+
return null;
2327
}
28+
const onlyNumberList = list.filter(item => typeof item === 'number' && !isNaN(item));
2429

25-
return list[middleIndex];
30+
if (!onlyNumberList.length) {
31+
return null;
32+
} else {
33+
const sortedList = onlyNumberList.sort((current, next) => current - next);
34+
const indexNearMiddleOfArray = Math.floor(sortedList.length / 2);
35+
if (sortedList.length % 2 === 0) {
36+
return (sortedList[indexNearMiddleOfArray - 1] + sortedList[indexNearMiddleOfArray]) / 2;
37+
} else {
38+
return sortedList[indexNearMiddleOfArray];
39+
}
40+
}
2641
}
2742

2843
module.exports = calculateMedian;

‎Sprint-1/fix/median.test.js‎

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,42 @@
77
const calculateMedian = require("./median.js");
88

99
describe("calculateMedian", () => {
10-
it("returns the median for odd length array", () => {
11-
expect(calculateMedian([1, 2, 3])).toEqual(2);
12-
expect(calculateMedian([1, 2, 3, 4, 5])).toEqual(3);
13-
});
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+
);
1418

15-
it("returns the average of middle values for even length array", () => {
16-
expect(calculateMedian([1, 2, 3, 4])).toEqual(2.5);
17-
expect(calculateMedian([1, 2, 3, 4, 5, 6])).toEqual(3.5);
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+
].forEach(({ input, expected }) =>
25+
it(`returns the correct median for unsorted array [${input}]`, () => expect(calculateMedian(input)).toEqual(expected))
26+
);
1927

20-
it("doesn't modify the input", () => {
28+
it("doesn't modify the input array [1, 2, 3]", () => {
2129
const list = [1, 2, 3];
2230
calculateMedian(list);
23-
2431
expect(list).toEqual([1, 2, 3]);
2532
});
26-
});
33+
34+
[ 'not an array', 123, null, undefined, {}, [], ["apple", null, undefined] ].forEach(val =>
35+
it(`returns null for non-numeric array (${val})`, () => expect(calculateMedian(val)).toBe(null))
36+
);
37+
38+
[
39+
{ input: [1, 2, "3", null, undefined, 4], expected: 2 },
40+
{ input: ["apple", 1, 2, 3, "banana", 4], expected: 2.5 },
41+
{ input: [1, "2", 3, "4", 5], expected: 3 },
42+
{ input: [1, "apple", 2, null, 3, undefined, 4], expected: 2.5 },
43+
{ input: [3, "apple", 1, null, 2, undefined, 4], expected: 2.5 },
44+
{ input: ["banana", 5, 3, "apple", 1, 4, 2], expected: 3 },
45+
].forEach(({ input, expected }) =>
46+
it(`filters out non-numeric values and calculates the median for [${input}]`, () => expect(calculateMedian(input)).toEqual(expected))
47+
);
48+
});

0 commit comments

Comments
 (0)