Which fix removes the bug?
function sum(arr) {
let total;
for (const n of arr) total += n;
return total;
}
- Change
oftoin - Change
const ntolet n - Return
arr.length - Initialize
total = 0
total starts as undefined, so adding yields NaN; start at 0.