关于如果javascript中的条件

时间:2013-12-30 13:57:11

标签: javascript

console.log("Batman glares at you.");
var userAnswer = prompt("Are you feeling lucky, punk?");
if (userAnswer==="yes")
{
console.log("Batman hits you very hard. It's Batman and you're you! Of course Batman wins!");
}
else
{
console.log("You did not say yes to feeling lucky. Good choice! You are a winner in the game of not getting beaten up by Batman.");
}

这里我想运行命令,如果userAnswer对第一个console.log语句采用除yes之外的多个值 即 我想指定“ya”以及1st console.log语句的userAnswer

4 个答案:

答案 0 :(得分:2)

console.log("Batman glares at you.");
var userAnswer = prompt("Are you feeling lucky, punk?");
if (["yes","ya","yo","yeaaaa","list goes on here"].indexOf(userAnswer)!=-1)
{
console.log("Batman hits you very hard. It's Batman and you're you! Of course Batman wins!");
}
else
{
console.log("You did not say yes to feeling lucky. Good choice! You are a winner in the game of not getting beaten up by Batman.");
}

答案 1 :(得分:1)

userAnswer = userAnswer.toLowerCase(); //Convert string to lower case. "Yes" ==> "yes"
userResponsesArray = ["yes","ya","yup","yeah"];
if (userResponsesArray.indexOf(userAnswer) !== -1) // This will check if the user answer exists in the list of acceptable responses. 
{
 // If the user answer is an acceptable response, come here.
}

链接:Array.IndexoftoLowerCase

答案 2 :(得分:0)

if (userAnswer==="yes" || userAnswer==="ya")

答案 3 :(得分:0)

在if:

中添加更多选项
if (userAnswer==="yes" || userAnswer==="yea" || userAnswer==="yup"){
    console.log("Batman hits you very hard. It's Batman and you're you! Of course Batman wins!");
}

||表示“或”。所以,如果答案是"yes",或答案是"yea"等等......