Spot the Logic Error
Question
This code should calculate the average of three numbers, but it gives the wrong answer. What is wrong?
Code
let avg = 10 + 20 + 30 / 3;
console.log(avg);
Hint
Check operator precedence: / binds tighter than +.
Explanation
Due to precedence, 30 / 3 = 10 is computed first, then 10 + 20 + 10 = 40. The correct average is (10 + 20 + 30) / 3 = 20. The fix is to add parentheses: let avg = (10 + 20 + 30) / 3;. This is a logic error — the code runs without crashing but gives the wrong result.
Related Resources
Related Lessons
- Errors
Learn about common programming errors.
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.