Which fix removes the bug?
const items = ['a', 'b', 'c'];
for (let i = 0; i <= items.length; i++) {
console.log(items[i].toUpperCase());
}
- Change
<=to< - Start the index at 1
- Use
items.length - 1 - Guard with a try block
The final iteration reads index 3, which is undefined, so the method call throws.
Play Problem Solving ▸