Learn advanced array methods by building a statistics calculator step 38

Blockquote
There are a few edge cases to account for when calculating the mode of a dataset. First, if every value appears the same number of times, there is no mode.
To calculate this, you will use a Set. A Set is a data structure that only allows unique values. If you pass an array into the Set constructor, it will remove any duplicate values.
Start by creating an if statement. In the condition, create a Set with new Set() and pass it the Object.values() of your counts object. If the size property of this Set is equal to 1, that tells you every value appears the same number of times. In this case, return null from your function.

My code:

const getMode = (array) => {
  const counts = {};
  array.forEach(el => counts[el] = counts[el] ? counts[el] + 1 : 1);
    if(new Set(Object.values(counts)).size===1){
      return null
    }
  }
}

What am I doing wrong?
Thanks in advance
Link to step 38

Hi @gurvirsanghera52

SyntaxError: unknown: Unexpected token (19:0)

  17 |     }
  18 |   }
> 19 | }
     | ^
  20 |
  21 |
  22 | const calculate = () => {

The console is giving you some information.

Happy coding

Thanks you so much, I wasnt paying enough attention!

1 Like