Javascript 如何根据多种可能性测试变量?

Javascript 如何根据多种可能性测试变量?,javascript,Javascript,我想测试一个变量,它是由用户输入的一系列选项决定的,如果这些选项中的任何一个是相等的,请转到下一个if问题。如果第二个用户的输入与这些相同的选项列表显示文本相同,则继续与其他选项列表进行比较 var nowWeather = document.getElementById("currentWeather").value; var todayWeather = document.getElementById("weatherToday").v

我想测试一个变量,它是由用户输入的一系列选项决定的,如果这些选项中的任何一个是相等的,请转到下一个if问题。如果第二个用户的输入与这些相同的选项列表显示文本相同,则继续与其他选项列表进行比较

var nowWeather = 
   document.getElementById("currentWeather").value;
var todayWeather = 
   document.getElementById("weatherToday").value;


if (nowWeather == "hot" 
 || nowWeather == "sunny"  
 || nowWeather == "warm"){
    if (todayWeather == "hot" 
     || todayWeather == "sunny" 
     || todayWeather == "warm") {
            text = "You should wear sandals today!";
     }
        else if (todayWeather == "wet" 
             || todayWeather == "rain" 
             || todayWeather == "rainy") {
                text = "You should wear goloshes today!";
        }
        else if (todayWeather == "cold" 
             || todayWeather == "snowy" 
             || todayWeather == "snow") {
                text = "You should bring boots today!";
        }
        else {
            text = "You should just wear some shoes today";
        }

}

逻辑OR仍然是最好的解决方案,除非您更喜欢像“热”、“晴”、“暖”这样的东西。indexOf(nowWeather)>-1

您是否有错误,或者您的代码没有按预期工作?如果它不工作,它在做什么?in运算符是JavaScript中的内置运算符,用于检查对象中是否存在特定属性。如果指定的属性在对象中,则返回布尔值true,否则返回false。一切正常。对不起,我完全忘了放我想要的lol。除了每次使用OR(| | |)之外,还有没有更干净的方法来测试nowWeather和todayWeather的字符串列表?对于每种可能性(即热、湿、冷),我想根据几个不同的列表测试用户输入。我猜这可能是因为。我曾考虑过使用该选项,但我认为如果没有更干净的方法,我更愿意坚持使用或| |。我可以对照数组检查用户输入吗?是的,我的答案告诉你怎么做。我不明白你的意思。