Step-40 Project statistics calculator

Click here to go to the problem.

Ask the problem: Now you need to sort the values properly. Chain the .sort() method to your Object.keys() call.
For the callback, you’ll need to use the counts object to compare the values of each key. You can use the a and b parameters to access the keys. Then, return the value of counts[b] minus the value of counts[a].
Finally, access the first element in the array using bracket notation to complete your highest variable.

const getMode = (array) => {
  const counts = {};
  array.forEach((el) => {
    counts[el] = (counts[el] || 0) + 1;
  })
  if (new Set(Object.values(counts)).size === 1) {
    return null;
  }
  const highest = Object.keys(counts).sort((a, b)=> counts[b] - counts[a]);
   return highest[0];
}

I write that code but the error is occurring.
Error: Your highest variable should have the value of the first entry in the sorted Object.keys(counts) array.
Why this error is occurring and how can be solved this error.
Please help me.

Click here to go the problem directly

Hi. Your code for the final instruction is wrong. You need to access the first element using bracket notation as part of your code representing the value of “highest”. You have done it on a new line. Remove the last line and work on the code in the highest variable. You have the correct index number. I found it useful to attempt it and then follow the hints when submitting the code to arrive at the right placement.

1 Like