Skip to content

Commit 7b99611

Browse files
committed
modified the function of median
1 parent 97e2315 commit 7b99611

1 file changed

Lines changed: 4 additions & 5 deletions

File tree

‎Sprint-1/fix/median.js‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,13 @@ function calculateMedian(list) {
2121
const sortedList = [...list].sort((a, b) => a - b);
2222

2323
if (sortedList.length % 2 === 0) {
24-
const evenMiddleIndex = Math.floor(sortedList.length / 2);
24+
const evenMiddleIndex = sortedList.length / 2;
2525
const sumOfEvenMiddleIndex =
26-
sortedList.splice(evenMiddleIndex, 1)[0] +
27-
sortedList.splice(evenMiddleIndex - 1, 1)[0];
26+
sortedList[evenMiddleIndex - 1] + sortedList[evenMiddleIndex]
2827
return sumOfEvenMiddleIndex / 2;
2928
} else {
30-
const oddMiddleIndex = Math.ceil(sortedList.length / 2);
31-
return sortedList.splice(oddMiddleIndex - 1, 1)[0];
29+
const oddMiddleIndex = Math.floor(sortedList.length / 2);
30+
return sortedList[oddMiddleIndex];
3231
}
3332
}
3433

0 commit comments

Comments
 (0)