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

Tell us what’s happening:

I can’t see where did I go wrong…

Your code so far

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

}

As far as I can see there are three small mistakes. First you used the let method to create the variable. Usually the program wants it to be a const variable and it won’t accept the solution even if it’s otherwise correct.
Second there should be an array, meaning [ ] arond the sorted[…/2-1] part.
Third you used sorted.length instead of array.length in the last part.
It should look like this:

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

The getMean function takes an array as an argument, so you need to surround all this statement sorted[array.length/2],sorted[array.length/2-1] into [ ].

Also, to bypass the test you need to use the array’s length property and you did except for the last one:

Oh yes. Thank you so much!!!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.