Which fix removes the bug?
const counts = {};
for (const c of "hello") {
counts[c]++;
}
- Change
++to+= 2in the body - Change
oftoinin the loop - Initialize
counts = []as an array - Use
counts[c] = (counts[c] || 0) + 1
Incrementing undefined gives NaN; default to 0 first.
Play Problem Solving ▸