Skip to content

Commit 28a696f

Browse files
reverted to original
1 parent 4222cda commit 28a696f

1 file changed

Lines changed: 25 additions & 3 deletions

File tree

‎Sprint-1/fix/median.js‎

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,31 @@
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 (!Array.isArray(list)) {
10+
return null;
11+
}
12+
if (list.length === 0) {
13+
return null;
14+
}
15+
const numbersOnly = list.filter((item) => typeof item === "number");
16+
if (numbersOnly.length === 0) {
17+
return null;
18+
}
19+
const sorted = [...numbersOnly].sort((a, b) => a - b);
20+
const middleIndex = Math.floor(sorted.length / 2);
21+
if (sorted.length % 2 === 0) {
22+
const median = (sorted[middleIndex - 1] + sorted[middleIndex]) / 2;
23+
return median;
24+
}
25+
return sorted[middleIndex];
1226
}
27+
/*
28+
const middleIndex = Math.floor(list.length / 2);
29+
const median = list.splice(middleIndex, 1)[0];
30+
return median;
31+
*/
1332

1433
module.exports = calculateMedian;
34+
/*
35+
36+
*/

0 commit comments

Comments
 (0)