let arr = [3, 5, 5];
let map = {};
for (let i of arr) {
if(map[i]){
map[i] = map[i]++ //<== doesn't work correctly with ++
}else{
map[i] = 1
}
}
console.log(map);
//outputs {3: 1, 5: 1}
Code above outputs {3: 1, 5: 1}, which is incorrect. 5 should be 2, not 1
let arr = [3, 5, 5];
let map = {};
for (let i of arr) {
if(map[i]){
map[i] = map[i]+1 // <== here it works correctly with +1
}else{
map[i] = 1
}
}
console.log(map);
//outputs {3: 1, 5: 2}
Code above outputs {3: 1, 5: 2} correct solution, but why the difference between the two solutions? I thought the ++ is equivalent to +1. But map[i]++ and map[i]+1 give different solutions!