← Back to Arena

"Debug this code: for(i=0; i<=arr.length; i++) { console.log(arr[i]); }"

4
Models
2,890
Total Votes
ChatGPT
Winner
ChatGPT ChatGPT Winner
1,203 votes (41.6%)
Bug found: Off-by-one error. Problem: i <= arr.length should be i < arr.length Arrays are 0-indexed, so arr.length gives you one more than the last valid index. Fixed code: for(let i = 0; i < arr.length; i++) { console.log(arr[i]); }
Claude Claude
987 votes (34.2%)
Two issues: First, you're missing 'let' or 'var' before 'i', which creates a global variable (bad practice). Second, your condition is i<=arr.length but arrays are zero-indexed, so you'll access arr[arr.length] which is undefined. Change to: for(let i = 0; i < arr.length; i++)
Gemini Gemini
498 votes (17.2%)
Classic off-by-one! Your loop runs one time too many. If arr has 3 items (indexes 0,1,2), arr.length is 3, so you're trying to access arr[3] which doesn't exist. Use < instead of <=. Also declare i with let.
Llama Llama
202 votes (7.0%)
Change <= to < in your loop condition. Arrays start at 0 so the last index is length-1. Also add 'let' before i.

Cast Your Vote

Which AI gave the best response? Your vote counts!

Thanks for voting!
Your vote has been recorded