我正在尝试编写一个简单的表单,要求以下输入:
情绪、年龄和性别
我在情绪提示中放了什么并不重要。它总是积极的。对于年龄,如果他们未满 18 岁,我希望选项卡关闭。对于性别,情绪提示也是如此。任何帮助将不胜感激。
var userMood = prompt("Hey! How's it going?");
if (userMood = "good" || "Good") {
alert("Awesome! We just need you to fill out a form for security reasons.");
} else if (userMood != "good" || "Good") {
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" ) {
alert("Great! You are the sufficient age to enter this webpage!");
userAge = true;
} else if ( userAge <= "17"){
alert("Sorry, you are too young to view this content...");
window.close();
}
var userGender = prompt ("What is your gender?");
if (userGender = "male" || "Male" || "female" || "Female"){
alert("Great! You're human!");
userGender = true;
} else {
alert("The webpage has perdicted non-human cyber activity, you can no longer continue.");
window.close();
}
我将使我的答案冗长以帮助您学习。 对于初学者来说,当您打算使用==
甚至===
时,您正在使用=
。 这就是为什么心情总是注册为好,因为userMood = "好"将userMood设置为"好",因此表达式的计算结果为"好",这将验证if语句,因为字符串的FALSE是",而true是每隔一个字符串。userMood = "good" || "Good"
也不会做你认为它做的事情。您需要像这样检查两个字符串(userMood === "good" || userMood === "Good")
.
我尝试将代码中的某些行更改为下面的内容。
var userMood = prompt("Hey! How's it going?");
if (userMood == "good" || userMood == "Good")
alert("Awesome! We just need you to fill out a form for security reasons.");
else
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 )
{
alert("Great! You are the sufficient age to enter this webpage!");
userAge = true;
}
else if ( userAge < 18 && userAge > 0)
{
alert("Sorry, you are too young to view this content...");
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")
{
alert("Great! You're human!");
userGender = true;
}
else
{
alert("The webpage has perdicted non-human cyber activity, you can no longer continue.");
window.close();
}