Spot the Loop Mistake
Find the MistakeEasy
Question
This loop is supposed to print 'hello' three times, but it only prints twice. What is wrong?
Code
for (let i = 0; i < 2; i++) {
console.log("hello");
}
Hint
How many values does i take: 0, 1 — that is only two iterations.
Explanation
With i < 2, i takes values 0 and 1 — only two iterations, so "hello" prints twice. To print three times, change the condition to i < 3 (i = 0, 1, 2) or i <= 2. This is a classic off-by-one error, the most common bug in loop logic.
Related Resources
Related Lessons
- Loops (for & while)
Learn how for and while loops work.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.