2017-08-19 40 views
0

我想編寫一個簡單的表單,詢問以下輸入:爲什麼這個表單代碼不起作用?這似乎是合乎邏輯

情緒,年齡和性別

不要緊,什麼我把心情提示。它總是出現積極的。對於這個年齡段,如果他們年齡在18歲以下,我希望標籤關閉。對於性別而言,它與情緒提示一樣。任何幫助,將不勝感激。

var userMood = prompt("Hey! How's it going?"); 
 
\t if (userMood = "good" || "Good") { 
 
\t \t alert("Awesome! We just need you to fill out a form for security reasons."); 
 
\t } else if (userMood != "good" || "Good") { 
 
\t \t alert("Oh, I'm sorry to hear that... We just need you to fill out a form for security reasons."); 
 
} 
 

 
var userAge = prompt("What is your current age?"); 
 
\t if (userAge >= "18") { 
 
\t \t alert("Great! You are the sufficient age to enter this webpage!"); 
 
\t \t userAge = true; 
 
\t } else if (userAge <= "17"){ 
 
\t \t alert("Sorry, you are too young to view this content..."); 
 
\t \t window.close(); 
 
} 
 
var userGender = prompt ("What is your gender?"); 
 
\t if (userGender = "male" || "Male" || "female" || "Female"){ 
 
\t \t alert("Great! You're human!"); 
 
\t \t userGender = true; 
 
\t } else { 
 
\t \t alert("The webpage has perdicted non-human cyber activity, you can no longer continue."); 
 
\t \t window.close(); 
 
\t }

回答

1

我要去把我的回答詳細,以幫助你學習。對於初學者,當您的意思是使用==甚至===時,您正在使用=。這就是爲什麼情緒總是處於良好狀態,因爲userMood =「good」會將userMood設置爲「good」,因此表達式評估爲「good」,這會驗證if語句,因爲字符串FALSE是「」,而且每個其他串。 userMood = "good" || "Good"也不會做你認爲的事情。你需要像這樣(userMood === "good" || userMood === "Good")檢查這兩個字符串。

0

我試着在你的代碼中改變一些行到下面的東西。

var userMood = prompt("Hey! How's it going?"); 
 
    if (userMood == "good" || userMood == "Good") 
 
\t  alert("Awesome! We just need you to fill out a form for security reasons."); 
 
    else 
 
\t  alert("Oh, I'm sorry to hear that... We just need you to fill out a form for security reasons."); 
 

 

 
var userAge = prompt("What is your current age?"); 
 
    if (userAge >= 18) 
 
     { 
 
\t  alert("Great! You are the sufficient age to enter this webpage!"); 
 
\t  userAge = true; 
 
     } 
 
    else if (userAge < 18 && userAge > 0) 
 
     { 
 
\t  alert("Sorry, you are too young to view this content..."); 
 
\t  window.close(); 
 
     } 
 
    else if (userAge < 0) 
 
     { 
 
     alert(" ***** prompt message for inputting negative number ****");  
 
     window.close(); 
 
     } 
 

 
var userGender = prompt ("What is your gender?"); 
 
    if (userGender == "male" || userGender == "Male" || userGender == "female" || userGender == "Female") 
 
     { 
 
\t  alert("Great! You're human!"); 
 
\t  userGender = true; 
 
     } 
 
    else 
 
     { 
 
\t  alert("The webpage has perdicted non-human cyber activity, you can no longer continue."); 
 
\t  window.close(); 
 
     }

+0

也許你也想考慮加入其他變化「好」,如'gooD'或'GooD'等。我想總共需要16個樣本。除非有人編碼以更簡單的方式做到這一點。 –

+0

一切正常,我喜歡你添加的空格,但不知何故,如果用戶年齡無效,程序仍然不會關閉窗口?幫助,將不勝感激 –

+0

好的,請稍候。我將編輯。我添加了'window.close();'if userAge <0'。 –

相關問題