簡體   English   中英

我試圖弄清楚如何讓我的函數一個接一個地運行……但是它不起作用

[英]I am trying to figure out how to make my functions run one after another… but it is not working

我正在嘗試使用JavaScript創建一個非常簡單的基於文本的冒險游戲。 我是個新手,我覺得與某些函數,變量和if-else語句一起使用會很棒。 我寫了一些代碼來提示屏幕詢問年齡並給出結果。

confirm("Ready to play?");
var age = prompt("How old are you?");
if (age >= 18) {
    document.write(" "); 
} else {
    document.write("You're less than 18? You better just stay home.");
}

document.write("<button> Sounds like you're ready.</button>");

var gender = prompt("Male","Female");
if(gender = "Male") {
    document.write("Sounds good!");
} else if(gender = "Female") {
    document.write("Sounds good!");
} else if {
    document.write("You're a damn liar");
    };
}; 

如果我只運行代碼的前半部分,那么一切都會正常運行。 但是,如果我嘗試在原始代碼的旁邊運行性別提示,它將不會運行。 有什么建議么?

此代碼應工作:

confirm("Ready to play?");
var age = prompt("How old are you?");
if (age >= 18) {
    document.write(" "); 
} else {
    document.write("You're less than 18? You better just stay home.");
}

document.write("<button> Sounds like you're ready.</button>");

var gender = prompt("Are you Male or Female?");
if(gender == "Male") {
    document.write("Sounds good!");
} else if(gender == "Female") {
    document.write("Sounds good!");
} else {
    document.write("You lie...");
} 

基本上,您需要將=替換= ===== 而且prompt()只接受一個參數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM