LearnFrenzy
Sign in Take a free test

Operators · Exercise 3 of 4

Operators Find the Error

  • 15 questions
  • Page 2 of 2
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Debug fifteen JavaScript programs about operators: SyntaxErrors from ??, ** and invalid assignment targets, TypeErrors from in, instanceof and BigInt, and silent bugs from ||, string concatenation, precedence, chained comparisons and NaN checks.

Question 11 Easy

The code below runs without an error, but its result is wrong. Which statement correctly identifies the bug?

main.js
let total = 0;
const values = ["10", "20", "30"];     // e.g. read from input fields
for (const v of values) {
  total += v;
}
console.log("Total:", total);
A After the first step total is the string "010", so += keeps concatenating – use total += Number(v)
B total must be declared with const
C for…of cannot loop over strings
D += only works with numbers
Question page & discussion
Question 12 Easy

The code below runs without an error, but its result is wrong. Which statement correctly identifies the bug?

main.js
const a = 60, b = 70, c = 80;
const average = a + b + c / 3;
console.log("Average:", average);
A The numbers should be strings
B average should be declared with let
C / has higher precedence than +, so only c is divided by 3 – write (a + b + c) / 3
D The result needs toFixed()
Question page & discussion
Question 13 Medium

The code below runs without an error, but its result is wrong. Which statement correctly identifies the bug?

main.js
const temp = 45;
if (10 < temp < 30) {
  console.log("Comfortable");
} else {
  console.log("Extreme");
}
A < cannot compare a number with a variable
B temp must be a string
C 10 < temp gives true, and true < 30 is 1 < 30 – always true; write temp > 10 && temp < 30
D The else branch needs braces
Question page & discussion
Question 14 Medium

The code below runs without an error, but its result is wrong. Which statement correctly identifies the bug?

main.js
function grade(score) {
  if (score === null) return "No score";
  return score >= 40 ? "Pass" : "Fail";
}
console.log(grade(null), grade(undefined), grade(55));
A null cannot be compared with ===
B The ternary needs parentheses
C === null misses undefined (a missing argument), and undefined >= 40 is false – use score == null
D grade must return a number
Question page & discussion
Question 15 Easy

The code below runs without an error, but its result is wrong. Which statement correctly identifies the bug?

main.js
const result = Number("abc");
if (result === NaN) {
  console.log("Invalid number");
} else {
  console.log("Result:", result);
}
A Number("abc") returns 0
B NaN is not equal to anything, including NaN, so the check is always false – use Number.isNaN(result)
C === cannot compare numbers
D The else branch prints the wrong variable
Question page & discussion
Up next · Exercise 4Operators Coding Problems10 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links