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

Tell us what’s happening:

Declare a median variable. Using the ternary operator, check if the length of array is even. If the length of array is even, find the two middle numbers and calculate the mean of those numbers. If the length of array is odd, find the middle number and assign it to the median variable.

The hint has this part that i don’t understand:

Your first argument should be the value of sorted at array.length / 2, and the second at array.length / 2 - 1.

i tried so many codes but still can’t pass it.

Your code so far

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

Console log

If the array.length is even, your median variable should use the getMean function to calculate the mean of the two middle numbers. Your first argument should be the value of sorted at array.length / 2, and the second at array.length / 2 - 1. If the array.length is odd, your median variable should assign the middle number to the median variable. The middle number is the value of sorted at Math.floor(array.length / 2)

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:121.0) Gecko/20100101 Firefox/121.0

Challenge Information:

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

You aren’t indexing into the sorted array here.

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

i tried to do the same in the function call but gives me error

Ok, but you still aren’t using the sorted array

this is the best i can do but nothing.

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

The logic looks right now. But count your s and ()s. They must come in pairs

i was missing one ],
i rewrote the code but still can’t pass it.

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

Ah, the test doesn’t like the use of Math.floor() here. If array.length is even, then array.length / 2 is a whole number.

1 Like