如何针对多种可能性测试变量?

时间:2020-10-23 15:53:52

标签: javascript

我希望根据一个选项列表来测试由用户输入确定的变量,如果这些选项中的任何一个相等,请转到下一个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";
        }

}

1 个答案:

答案 0 :(得分:0)

除非您更喜欢||之类的东西,否则逻辑或['hot', 'sunny', 'warm'].indexOf(nowWeather) > -1仍然是最佳解决方案