Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix:#758 Average Median code cleanup #1388

Merged
merged 6 commits into from
Oct 2, 2023
Merged
Changes from 5 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
16 changes: 4 additions & 12 deletions Maths/AverageMedian.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,14 @@
*/

const averageMedian = (sourceArrayOfNumbers) => {
let numbers = [...sourceArrayOfNumbers]
let median = 0
const numbers = [...sourceArrayOfNumbers].sort(sortNumbers)
const numLength = numbers.length
numbers = numbers.sort(sortNumbers)

if (numLength % 2 === 0) {
developerr-ayush marked this conversation as resolved.
Show resolved Hide resolved
median = (numbers[numLength / 2 - 1] + numbers[numLength / 2]) / 2
} else {
median = numbers[(numLength - 1) / 2]
}

return median
return (numbers[numLength / 2 - 1] + numbers[numLength / 2]) / 2
} else return numbers[Math.floor(numLength / 2)]
developerr-ayush marked this conversation as resolved.
Show resolved Hide resolved
}

const sortNumbers = (num1, num2) => {
return num1 - num2
}
const sortNumbers = (num1, num2) => num1 - num2
developerr-ayush marked this conversation as resolved.
Show resolved Hide resolved

export { averageMedian }