Skip to content

Commit cff19af

Browse files
reverted to original
1 parent 28a696f commit cff19af

1 file changed

Lines changed: 3 additions & 25 deletions

File tree

‎Sprint-1/fix/median.js‎

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

88
function calculateMedian(list) {
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];
9+
const middleIndex = Math.floor(list.length / 2);
10+
const median = list.splice(middleIndex, 1)[0];
11+
return median;
2612
}
27-
/*
28-
const middleIndex = Math.floor(list.length / 2);
29-
const median = list.splice(middleIndex, 1)[0];
30-
return median;
31-
*/
3213

3314
module.exports = calculateMedian;
34-
/*
35-
36-
*/

0 commit comments

Comments
 (0)