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

Tell us what’s happening:

I have a problem within my ternary operator I can´t do this step. Could someone please explain to me what I am doing wrong?

// User Editable Region

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

Your browser information:

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

Challenge Information:

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

What error message are you getting? Looking carefully at the exact wording of the error message can often help.

If array.length is even, pass an array with the two middle numbers from the sorted array to the getMean function. The first item in the array argument should be the value at index array.length / 2 from sorted and the second should use the value at the index array.length / 2 - 1 from sorted .

Right now you aren’t accessing the value of those indexes,rather you are sending the indexes themselves. How do you access the value at an index in an array?

Note the words “sorted array” - it wants an array passed in

const median = array.length % 2 === 0 ? getMean(sorted[array.length / 2], [array.length / 2 - 1]) : sorted[Math.floor(array.length / 2)];
I know that the getMean function receives an ordered array which is “sorted” but I can’t find a way to pass the data to sorted :frowning:

You’re so close - remember that sorted is an array so all the ‘arguments’ to sorted should be arrays (you have 2 out of 3 there) but also that getMean is expecting an array

const median = array.length % 2 === 0 ? getMean(sorted[array.length / 2] , sorted[array.length / 2 - 1]): sorted[Math.floor(array.length / 2)];
I tried this logic but I don´t know if I’m broking more my code.

getMean takes only one argument–an array. Your code has two arguments in the getMean function.

Thanks for your advices! It’s working now :slight_smile: