[英]Stop a 'falsy' alert after a correct prompt in JavaScript
正確回答提示后,給出正確答案后,仍然會出現錯誤答案的警報消息。 我似乎不知道為什么也不知道如何終止第二次警報。
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."); };
格式/代碼樣式錯誤, 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."); }
另請注意,在代碼塊( {}
)之后不需要分號( ;
)。 實際上,在您的情況下,一個if
- else if
鏈將被破壞。
您的代碼有效! 我認為您必須輸入錯誤的代碼,因為它區分大小寫。 我將其添加為不區分大小寫:
if (userResponse === "Boston" || userResponse === "boston") {
現在,您可以鍵入boston
而不是只能鍵入Boston
。 我的建議是永遠不要使答案僅區分大小寫! 在這種情況下, I am sorry, but boston is not the right answer.
,當它完全正確時
並確保在@AurelBily指出的第二種可能性上添加else
內容!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.