Skip to content

Commit 95f7fd3

Browse files
committed
Add calculateMedian function with tests for even and odd length arrays
1 parent c9fbbff commit 95f7fd3

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

prep/mean.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,7 @@ function calculateMean(list) {
33
return sum / list.length;
44
}
55

6-
module.exports = calculateMean;
6+
module.exports = calculateMean;
7+
8+
9+
// module.exports = { calculateMean };

prep/median.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/*
12
// prep/median.js
23
function calculateMedian(list) {
34
const middleIndex = Math.floor(list.length / 2);
@@ -6,4 +7,23 @@ function calculateMedian(list) {
67
return median;
78
}
89
module.exports = calculateMedian;
10+
*/
11+
12+
// We clone the array with [...numbers]
13+
// before sorting to avoid mutating the original input.
14+
15+
16+
function calculateMedian(numbers) {
17+
const sorted = [...numbers].sort((a, b) => a - b);
18+
const mid = Math.floor(sorted.length / 2);
19+
20+
if (sorted.length % 2 === 0) {
21+
return (sorted[mid - 1] + sorted[mid]) / 2;
22+
} else {
23+
return sorted[mid];
24+
}
25+
}
26+
27+
module.exports = calculateMedian;
28+
929

prep/median.test.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
// const { calculateMedian } = require("./median");
12
const calculateMedian = require("./median");
3+
4+
25
const calculateMean = require("./mean");
6+
// const { calculateMean } = require("./mean");
37

48
test("calculates the median of a list of odd length", () => {
59
const list = [10, 20, 30, 50, 60];
@@ -9,6 +13,22 @@ test("calculates the median of a list of odd length", () => {
913
expect(currentOutput).toEqual(targetOutput);
1014
})
1115

16+
test("calculates the median of an odd-length array", () => {
17+
const list = [3, 50, 7];
18+
const currentOutput = calculateMedian(list); // sorted = [3, 7, 50], median = 7
19+
const expectedOutput = 7;
20+
21+
expect(currentOutput).toEqual(expectedOutput);
22+
});
23+
24+
test("calculates the median of a list of odd length", () => {
25+
const list = [10, 20, 30, 40];
26+
const currentOutput = calculateMedian(list);
27+
const targetOutput = 25;
28+
29+
expect(currentOutput).toEqual(targetOutput);
30+
})
31+
1232
// Both functions access the same array
1333
// because JavaScript passes objects and
1434
// arrays by reference — they all point to the same memory location.
@@ -18,4 +38,9 @@ const median = calculateMedian(salaries);
1838
const mean = calculateMean(salaries);
1939
console.log(salaries, "<--- salaries input before we call calculateMean");
2040
console.log(`The median salary is ${median}`);
21-
console.log(`The mean salary is ${mean}`);
41+
console.log(`The mean salary is ${mean}`);
42+
43+
44+
45+
46+

0 commit comments

Comments
 (0)