Question 14
Back to Exercise
The code below runs without an error, but its result is wrong. Which statement correctly identifies the bug?
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 Correct
- D case values must be in quotes only in strict mode
Correct Answer: Option C
Convert once before the switch: switch (Number(choice)), or write case "1": and case "2":.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!