Skip to content

Commit 152abe7

Browse files
committed
completed median sprint 1
1 parent 96d077b commit 152abe7

File tree

2 files changed

+50
-8
lines changed

2 files changed

+50
-8
lines changed

Sprint-1/fix/median.js

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,39 @@
66
// or 'list' has mixed values (the function is expected to sort only numbers).
77

88
function calculateMedian(list) {
9-
const middleIndex = Math.floor(list.length / 2);
10-
const median = list.splice(middleIndex, 1)[0];
11-
return median;
9+
// if list is not an array return null.
10+
if (!Array.isArray(list)) {
11+
return null;
12+
}
13+
14+
// create a variable called numbers and store an empty array.
15+
let numbers = [];
16+
17+
// loop over each item in list.
18+
for (const item of list) {
19+
// if item is a number, push it into the numbers array.
20+
if (typeof item === "number") {
21+
numbers.push(item);
22+
}
23+
}
24+
25+
// if numbers array is empty return null.
26+
if (numbers.length === 0) {
27+
return null;
28+
}
29+
30+
// sort the numbers in the number array in ascending order.
31+
numbers.sort((a, b) => a - b);
32+
33+
// find the middle index and store it in a variable.
34+
const middleIndex = Math.floor(numbers.length / 2);
35+
36+
// if its an even array there are two middle indexes, so add them together and divide by two.
37+
if (numbers.length % 2 === 0) {
38+
return (numbers[middleIndex] + numbers[middleIndex - 1]) / 2;
39+
}
40+
// for odd arrays return middle index.
41+
return numbers[middleIndex];
1242
}
1343

1444
module.exports = calculateMedian;

Sprint-1/fix/median.test.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ describe("calculateMedian", () => {
1313
{ input: [1, 2, 3, 4], expected: 2.5 },
1414
{ input: [1, 2, 3, 4, 5, 6], expected: 3.5 },
1515
].forEach(({ input, expected }) =>
16-
it(`returns the median for [${input}]`, () => expect(calculateMedian(input)).toEqual(expected))
16+
it(`returns the median for [${input}]`, () =>
17+
expect(calculateMedian(input)).toEqual(expected))
1718
);
1819

1920
[
@@ -24,7 +25,8 @@ describe("calculateMedian", () => {
2425
{ input: [110, 20, 0], expected: 20 },
2526
{ input: [6, -2, 2, 12, 14], expected: 6 },
2627
].forEach(({ input, expected }) =>
27-
it(`returns the correct median for unsorted array [${input}]`, () => expect(calculateMedian(input)).toEqual(expected))
28+
it(`returns the correct median for unsorted array [${input}]`, () =>
29+
expect(calculateMedian(input)).toEqual(expected))
2830
);
2931

3032
it("doesn't modify the input array [3, 1, 2]", () => {
@@ -33,8 +35,17 @@ describe("calculateMedian", () => {
3335
expect(list).toEqual([3, 1, 2]);
3436
});
3537

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+
"not an array",
40+
123,
41+
null,
42+
undefined,
43+
{},
44+
[],
45+
["apple", null, undefined],
46+
].forEach((val) =>
47+
it(`returns null for non-numeric array (${val})`, () =>
48+
expect(calculateMedian(val)).toBe(null))
3849
);
3950

4051
[
@@ -45,6 +56,7 @@ describe("calculateMedian", () => {
4556
{ input: [3, "apple", 1, null, 2, undefined, 4], expected: 2.5 },
4657
{ input: ["banana", 5, 3, "apple", 1, 4, 2], expected: 3 },
4758
].forEach(({ input, expected }) =>
48-
it(`filters out non-numeric values and calculates the median for [${input}]`, () => expect(calculateMedian(input)).toEqual(expected))
59+
it(`filters out non-numeric values and calculates the median for [${input}]`, () =>
60+
expect(calculateMedian(input)).toEqual(expected))
4961
);
5062
});

0 commit comments

Comments
 (0)