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
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
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([])
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.