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 aSet
. ASet
is a data structure that only allows unique values. If you pass an array into theSet
constructor, it will remove any duplicate values.
Start by creating anif
statement. In the condition, create aSet
withnew Set()
and pass it theObject.values()
of yourcounts
object. If thesize
property of thisSet
is equal to1
, that tells you every value appears the same number of times. In this case, returnnull
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