Learn Advanced Array Methods by Building a Statistics Calculator - Step 22

Tell us what’s happening:

Describe your issue in detail here.

please someone should help me rewrite this code to pass the challenge. I have tried it many times.

Your code so far

  const sorted = array.sort((a, b) => a - b);
  const median = (array.length % 2 === 0) ? sorted[(array.length / 2 + array.length / 2 - 1)] : array.Math.floor(array.length);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36

Challenge Information:

Learn Advanced Array Methods by Building a Statistics Calculator - Step 22

You appear to have created this post without editing the template. Please edit your post to Tell us what’s happening in your own words.

Hi @edwin1

From the instructions:
image

Where is your code for the getMean function?

Happy coding

const getMedian = (array) => {
const sorted = array.sort((a, b) => a - b);

const median = (array.length % 2 === 0) ? [sorted[array.length / 2 ]+ sorted[array.length / 2 - 1]] : Math.floor(array.length / 2);
}

Yet it is not working

Hi Edwin!
As the directions say, you need to use the getMean function to get the average of the two numbers when length is even.

const median =
array.length % 2 === 0
? getMean([sorted[array.length / 2], sorted[array.length / 2 - 1]])
: sorted[Math.floor(array.length / 2)];

1 Like