Skip to content

Commit c9fbbff

Browse files
committed
Add test for calculateMedian and calculateMean using salaries array
1 parent b06cb19 commit c9fbbff

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

prep/median.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// prep/median.js
2+
function calculateMedian(list) {
3+
const middleIndex = Math.floor(list.length / 2);
4+
const median = list.splice(middleIndex, 1)[0];
5+
6+
return median;
7+
}
8+
module.exports = calculateMedian;
9+

prep/median.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const calculateMedian = require("./median");
2+
const calculateMean = require("./mean");
3+
4+
test("calculates the median of a list of odd length", () => {
5+
const list = [10, 20, 30, 50, 60];
6+
const currentOutput = calculateMedian(list);
7+
const targetOutput = 30;
8+
9+
expect(currentOutput).toEqual(targetOutput);
10+
})
11+
12+
// Both functions access the same array
13+
// because JavaScript passes objects and
14+
// arrays by reference — they all point to the same memory location.
15+
16+
const salaries = [10, 20, 30, 40, 60, 80, 80];
17+
const median = calculateMedian(salaries);
18+
const mean = calculateMean(salaries);
19+
console.log(salaries, "<--- salaries input before we call calculateMean");
20+
console.log(`The median salary is ${median}`);
21+
console.log(`The mean salary is ${mean}`);

prep/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
{
22
"name": "mean.test.js",
33
"version": "1.0.0",
4+
"scripts": {
5+
"test": "jest"
6+
},
7+
"name": "median.test.js",
8+
"version": "1.0.0",
49
"scripts": {
510
"test": "jest"
611
},

0 commit comments

Comments
 (0)