LearnFrenzy
Sign in Take a free test

Strings · Find the Error · Question 12

Bug: "yes" is not accepted Answer, explanation & discussion

  • Find the error
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 12 Back to Exercise

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

main.js
const answer = "yes";
if (answer === "Yes") {
  console.log("Subscribed");
} else {
  console.log("Not subscribed");
}
  • A === cannot compare strings
  • B The else branch always runs
  • C === is case-sensitive – compare answer.toLowerCase() === "yes" Correct
  • D answer must be declared with let
Correct Answer: Option C

"yes" and "Yes" are different strings. Normalise user input (trim and lower-case) before comparing.

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!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links