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

what’s happening: I don’t know anymore dude…
Describe your issue in detail here.
so I’ve been working on this step of this section for roughly 5-ish hours now? trying to get the average of the middle two numbers of the array. I’ve tried so many different combinations of code that it would be useless to post them all here but I will try to give you the general logic I’m think of

  1. we are checking if the array.length is even or odd based on the remainder operator and the ternary operator.

2 if the number is even we need to add the middle two numbers of the array and get the average, that will be the median. – (I am stuck on this part as I am unsure to correctly code this using the getMean function. it seems to only want one array? I’ve tried adding them together then dividing the sum by 2 but that doesn’t seem to work, maybe I’m just an idiot though)

3 if the number is odd the middle number is the median and math.floor will make sure the the array index is rounded down to the nearest whole number – I don’t think I have trouble understanding this part but at this point I’m throwing code spaghetti at the wall and seeing what sticks.

so if anyone could read through my logic, maybe find out where I’m going wrong that would be helpful, I’ve already read many of the other threads pertaining to this issue and none of them really helped me but I am willing to re read them in case I missed something

Your code so far

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

// User Editable Region

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/ 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.

just a brief update I came back and realized I may have deleted too much code from the opening post I’ve made a few changes to the previous code unfortunately I dont seem to be any closer to the solution.

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)]

thats the code so far, sadly its not correct, but hopefully I have the right idea?

You are closer than you think. Your code might pass if you substitute the + for a , and enclose the content of getMean() with another [] as getMean([])

1 Like

Tysm!!, I really don’t think I would have gotten it without your help! I had done so many variations of the previous code and I was beginning to lose my mind, interesting that all it needed was a comma and some extra brackets. also another question , why were the extra brackets needed? I’m a bit confused on that point as I’ve never actually seen any syntax or at least recall seeing syntax like that before.

getMean is defined here, right?

const getMean = (array) => array.reduce((acc, el) => acc + el, 0) / array.length;

It accepts an array. Therefore whatever was inside the parenthesis needed to be an array, thus [].

Part of the request was:

find the two middle numbers and calculate the mean of those numbers.

You needed two numbers (passed as an array to getMean) to calculate the mean. By using a + you are giving it only a number (because of the addition)