I have this code snippet which works
for (let [num, freq] of map) {
let values = (bucket[freq] || new Array()) ;
values.push(num);
bucket[freq] = values
}
bucket is an array of lists
it's iterating over map
, checking if there is a list in the spot of freq
, if not, it creates a new list, pushes num
to the list, and pushes the list back to bucket[freq]
.
When I tried to do it in one line, like this
for (let [num, freq] of map) {
bucket[freq] = (bucket[freq] || new Array()).push(num) ;
}
then bucket didn't update correctly. I can't understand what is the difference between the two approaches
I tried to write both versions, only the first worked, the other gave me incorrect answers