I get the expected result here.
var modeObj = {};
array.forEach(num => {
if (!modeObj[num])
modeObj[num] = 0;
modeObj[num]++;
});
I get an empty result here.
var modeObj = {};
array.forEach(num => {
if (!modeObj[num]) {
modeObj[num] = 0;
}else {
modeObj[num]++;
}
});
How is the above code different from the one below? I'm missing some concept in the if condition.