$ ./guess_the_languageProblem Solving — fix the bug
Problem Solving — fix the bug · easy · #14

Which fix removes the off-by-one bug?

const arr = [1, 2, 3];
for (let i = 0; i <= arr.length; i++) {
  console.log(arr[i]);
}
  1. Change i = 0 to i = 1 in the header
  2. Change i <= arr.length to i < arr.length
  3. Change i++ to i-- in the header
  4. Change arr[i] to arr[i + 1] in body
The loop must stop before length to avoid reading undefined.
Play Problem Solving ▸