Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,21 @@
// or 'list' has mixed values (the function is expected to sort only numbers).

function calculateMedian(list) {
const middleIndex = Math.floor(list.length / 2);
const median = list.splice(middleIndex, 1)[0];
return median;
// Filter out non-numeric values and sort the remaining numbers
const numbers = list
.filter((x) => typeof x === "number" && !isNaN(x))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you plan to include Infinity and -Infinity in the calculation?

.sort((a, b) => a - b);

if (numbers.length === 0) {
return null;
}

const mid = Math.floor(numbers.length / 2);
if (numbers.length % 2 === 0) {
return (numbers[mid - 1] + numbers[mid]) / 2;
} else {
return numbers[mid];
}
}

module.exports = calculateMedian;
Loading