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

Tell us what’s happening:

getMode function

I think the challenges asks for filling an empty object with keys that are the numbers (as strings of course in case of object keys) and their values should be the amount of occurrences of that number in an array.
I can’t seem to solve the problem with the usage of a single forEach call.
Whenever I do this, the object keys seem to be correct but the values are undefined or NaNs.

My JS

/* file: script.js */

const getMode = (array) => {
 const counts = {};
 array.forEach(el => counts[el] = 0)
 array.forEach(el => counts[el] += 1);
console.log(counts)

}

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

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)];
  return median;
}


/* User Editable Region */



/* User Editable Region */



const calculate = () => {
  const value = document.querySelector("#numbers").value;
  const array = value.split(/,\s*/g);
  const numbers = array.map(el => Number(el)).filter(el => !isNaN(el));
  
  const mean = getMean(numbers);
  const median = getMedian(numbers);

  document.querySelector("#mean").textContent = mean;
  document.querySelector("#median").textContent = median;
}

Your browser information:

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

Challenge Information:

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

This step is pretty unreasonable and I opened an issue for it some days ago. I haven’t heard anything back yet.

Even with the suggested change in the text, I’m not sure it will be good enough for people to solve this, at least not using the fixed solution it requires (it is using a regex that locks you into one solution).

Use el to create a computed property name on the counts object. Assign it a default value of 0 , if the property already exists increment it.

I would suggest you skip forward and look at the solution. In this case, it isn’t going to change anything as most people are fairly unlikely (in my opinion) to solve this as required.

2 Likes

Thanks for the hint, in retrospect, the “fallback” value is actually a good hint…

I do not get what you mean by regex. Where do we use regex in our getMode()?
I got solution from your open issue. However my solution before this was so close setting fallback straight to 1.
array.forEach((el) => counts[el] = counts[el] + 1 || 1);
It appears to work fine in test under VS Code. It seems very unnecessary that we have to initialize it to 0 then increment. I fail to see what is wrong with my solution that is even less characters and appears to work just fine, but maybe someone knowledgeable like you can let me know.

The test is using a regex, you are not asked to use a regex.

It should not matter how you write it as long as the final object is correct. Which requires us to work on actual data and check the object for correctness.

As said, there is an issue open for this and hopefully, it will be fixed at some point.

2 Likes

i tried this and it worked,

const getMode = (array) => {
  const counts = {};
  array.forEach((el) => {
    counts[el] = (counts[el] || 0) + 1;
  })

}
2 Likes

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