LearnFrenzy
Sign in Take a free test

Conditional Statements · Exercise 3 of 4

Conditional Statements Find the Error

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

Debug fifteen JavaScript programs with conditional statements: SyntaxErrors from misplaced semicolons, missing parentheses and duplicate defaults, runtime errors inside conditions, and silent bugs from missing breaks, ladder order, falsy values, strict switch matching and the dangling else.

Question 11 Easy

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

main.js
function price(size) {
  let p = 0;
  switch (size) {
    case "S": p = 100;
    case "M": p = 150;
    case "L": p = 200;
  }
  return p;
}
console.log(price("S"), price("M"), price("L"));
A p should be declared with const
B switch cannot compare strings
C The cases have no break, so every match falls through and ends with p = 200
D The function should use if instead of return
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
function discount(amount) {
  if (amount > 1000) return 5;
  else if (amount > 5000) return 10;
  else return 0;
}
console.log(discount(8000), discount(2000), discount(500));
A return cannot be used inside if
B The else is missing braces
C amount should be a string
D amount > 1000 is tested first, so the > 5000 branch can never be reached – test the larger threshold first
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
function showStock(count) {
  if (count) return `${count} in stock`;
  return "Stock unknown";
}
console.log(showStock(0), "|", showStock(12), "|", showStock(undefined));
A Template literals cannot contain numbers
B count must be converted with String()
C The function needs an else
D 0 is falsy, so a real stock of 0 is treated like a missing value – test count == null (or typeof count === "number")
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
const choice = "2";               // value read from a text input
switch (choice) {
  case 1: console.log("Check balance"); break;
  case 2: console.log("Withdraw"); break;
  default: console.log("Invalid choice");
}
A switch cannot have a default
B break must come before console.log
C Input values are strings and switch uses ===, so "2" never matches 2 – use switch (Number(choice)) or string cases
D case values must be in quotes only in strict mode
Question page & discussion
Question 15 Hard

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

main.js
function access(isLoggedIn, isAdmin) {
  if (isLoggedIn)
    if (isAdmin) return "Admin panel";
  else return "Please log in";
  return "Dashboard";
}
console.log(access(true, false), "|", access(false, false));
A Without braces the else belongs to if (isAdmin), so a logged-in non-admin is told to log in and a logged-out user reaches the dashboard
B return cannot be used inside nested ifs
C The parameters are in the wrong order
D isAdmin must be checked with ===
Question page & discussion
Up next · Exercise 4Conditional Statements Coding Problems10 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links