2016-07-28 28 views
0

我的JavaScript似乎很好(.prompt()和一切,甚至嵌套的if/else語句的開始),但是當我驗證我的JavaScript時,它提示「Expected semicolon before statement」爲第一個提示。提示方法不能用?

我看不出我的代碼是怎麼錯的。我試圖在提示符之前使用一個等號來定義變量,因爲我認爲如果我沒有這樣做,我的變量將是未定義的。我不能肯定地說我會有多少變數,所以我不能只提前提供變量列表。這是與我的性格個性測試。

這是代碼。性別變量應該只接受2個答案,男性和女性。 childrenh變量應該接受0到12之間的任何數字(12是我的角色每次懷孕的最大數量)。這是一個數組可能會派上用場或某些等效的,因爲那時我可能有導致更多.prompt(件)語句

function Character() { 
 
    var gender.prompt("What is your gender?"); 
 
    if (gender = "Female") { 
 

 
    var children_h.prompt("Have you had any children recently?"); 
 
    if (children_h = "0") { 
 
     var pregnant.prompt("Are you pregnant?"); 
 
     if (pregnant = "Yes") { 
 
     var pregnant_m.prompt("Is it a multiple pregnancy?"); 
 
     if (pregnant_m = "Yes") { 
 
      var pregnant_t.prompt("How far are you in your pregnancy(trimester)?"); 
 
      if (pregnant_t = "1") { 
 
      alert("Expect to feel like you are sick"); 
 
      }; 
 
      else if (pregnant_t = "2") { 
 
      alert("You might feel some movements"); 
 
      }; 
 
      else if (pregnant_t = "3") { 
 
      alert("You might feel pain before labor.") 
 
      }; 
 
      else { 
 
      alert("Your baby is more than ready. Don't be surprised if you go into labor.") 
 
      }; 
 
     }; 
 
     }; 
 
    }; 
 
    else if (children_h = "1") { 
 
     var children_a.prompt("How old is your child?") 
 
    }; 
 
    else if (children_h = "2") { 
 
     var chilren_m.prompt("Were they twins or not?") 
 
    }; 
 
    else if (children_h = "3") { 
 
     children_m.prompt("Were they twins or triplets?") 
 
    }; 
 
    else if (children_h = "4") { 
 
     children_m.prompt("Were they multiples?") 
 
    }; 
 
    else if (children_h = "5") { 
 
     children_m.prompt("Were they multiples?") 
 
    }; 
 
    else if (children_h = "6") { 
 
     children_m.prompt("Were they multiples?") 
 
    }; 
 
    else if (children_h = "7") { 
 
     children_m.prompt("Were they multiples?") 
 
    }; 
 
    else if (children_h = "8") { 
 
     children_m.prompt("Were they multiples?") 
 
    }; 
 
    else if (children_h = "9") { 
 
     children_m.prompt("Were they multiples?") 
 
    }; 
 
    else if (children_h = "10") { 
 
     children_m.prompt("Were they multiples?") 
 
    }; 
 
    else if (children_h = "11") { 
 
     children_m.prompt("Were they multiples?") 
 
    }; 
 
    else if (children_h = "12") { 
 
     children_m.prompt("Were they multiples?") 
 
    }; 
 
    else { 
 
     alert("Impossible!") 
 
    }; 
 
    }; 
 
    else if (gender = "Male") { 
 
    childrenh.prompt("Have you had any children recently?"); 
 
    }; 
 
    else { 
 
    alert("Undefined gender!") 
 
    }; 
 
};
<!DOCTYPE html> 
 
<html> 
 

 
<head> 
 
    <link rel='stylesheet' href='style.css' /> 
 
    <script src='script.js'></script> 
 
</head> 
 

 
<body> 
 
    <p>Take the personality test to find out your character's personality</p> 
 
    <button onclick="Character">Test</button> 
 
</body> 
 

 
</html>

(12 if/else語句本質上等同)那麼,爲什麼我的變量會出現語法錯誤?我用與聲明我的提示性陳述相同的語句聲明這些變量。

編輯:

我編輯的代碼,以包括更多的if/else語句和全球功能通過按鈕標籤連接到HTML。它仍然不起作用(單擊按鈕時字符未定義(即使我已將此函數定義爲if/else業務並將其放在HTML中的相同名稱中)並且預期;來當程序啓動時(我不明白這個特殊的if語句有什麼問題))。我還沒有任何CSS代碼。

我使用的是Codecademy,它爲所有的codeb構建了外部CSS和JS(這就是爲什麼腳本標記在標題中是因爲有了它,它應該引用外部JavaScript,只要它是呼籲(就像在這個按鈕,應該導致所有這些提示等等,當我完成它))。

回答

1

var gender.prompt("What is your gender?"); 
if(gender = "Female"){ 

    var childrenh.prompt("Have you had any children recently"); 
}; 

更改爲

var gender = prompt("What is your gender?"); 
if(gender === "Female"){   
    var children = prompt("Have you had any children recently"); 
}; 
+0

但我試過了,我得到所有這些「未申報的提示()語句」的錯誤。這就像從1錯誤到下一個錯誤。首先變量是未定義的,然後當我定義它時,prompt()語句是未聲明的。 – Caters

+0

你有改變「。」以「=」 – user86745458

+0

例如,更改var pregnant.prompt(「你懷孕了嗎?」);到var懷孕=提示(「你懷孕了嗎?」); – user86745458