Debug fifteen JavaScript loop programs: SyntaxErrors from break in callbacks, commas in a for header and missing labels, TypeErrors from non-iterables and off-by-one reads, and silent bugs from for…in on arrays, splice in a forward loop and forEach used to search.
Question 11Easy
The code below runs without an error, but its result is wrong. Which statement correctly identifies the bug?
main.js
const bills = [250, 400, 150];
let total = 0;
for (let i = 0; i <= bills.length; i++) {
total += bills[i];
}
console.log("Total:", total);
Atotal must be declared with const
Bi <= bills.length reads bills[3], which is undefined, and 800 + undefined is NaN – use i < bills.length
Cbills must be converted to numbers
D+= cannot be used in a loop
Correct Answer: Option B
The loop runs once too often. With < it prints Total: 800.
We use Google Analytics cookies to see how LearnFrenzy is used and make it better, and Google AdSense to keep it free.
No analytics or ad cookies are set until you accept. Privacy Policy
·