After answering the prompt correctly, the alert-message for wrong answer still appear after the right answer is given. I don't seem to find why nor how to terminate the second alert.
var userResponse = prompt("Hello, please enter the capital of Massachusetts?"); if(userResponse === "Boston") { alert("Yes! Boston is the right answer"); }; if(userResponse === "Albany"){ alert("I am sorry, but Albany is rather the capital of NY"); } else { alert("I am sorry, but" + userResponse + " is not the right answer."); };
Bad formatting / code style and a missing else
:
var userResponse = prompt("Hello, please enter the capital of Massachusetts?"); if (userResponse === "Boston") { alert("Yes! Boston is the right answer"); } else if (userResponse === "Albany") { alert("I am sorry, but Albany is rather the capital of NY"); } else { alert("I am sorry, but " + userResponse + " is not the right answer."); }
Also note semicolons ( ;
) are not necessary after code blocks ( {}
). In fact, in your case, one would have broken your if
- else if
chain.
Your code works! I think you must be simply typing it in wrong since it is case sensitive. I would add this so that it is not case sensitive:
if (userResponse === "Boston" || userResponse === "boston") {
Now you can type boston
instead of only being able to type Boston
. My recommendation is to never make the answer only case sensitive! In this case, it will say I am sorry, but boston is not the right answer.
, when it is totally correct
And make sure to add else
on the second possibility as @AurelBily pointed out!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.