$ ./guess_the_languageProblem Solving — fix the bug
Problem Solving — fix the bug · medium · #35

Which fix removes the bug?

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