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

Which fix removes the bug?

function sum(arr) {
  let total;
  for (const n of arr) total += n;
  return total;
}
  1. Change of to in
  2. Change const n to let n
  3. Return arr.length
  4. Initialize total = 0
total starts as undefined, so adding yields NaN; start at 0.
Play Problem Solving ▸