-1

I'm look to test a variable that is determined by a users input against a list of options, if any of those options are equal move on to the next if question. If the second users input is also equal against those same list of options display text, else move on to compare against other lists of options.

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";
        }

}
David
  • 208,112
  • 36
  • 198
  • 279
UnclePete
  • 25
  • 5
  • Are you having an error or is your code not working as expected? If its not working, what is it doing? – imvain2 Oct 23 '20 at 15:57
  • The **in** operator is an inbuilt operator in JavaScript which is used to check whether a particular property exists in an object or not. It returns boolean value true if the specified property is in an object, otherwise it returns false. – Rob Moll Oct 23 '20 at 16:00
  • Everything is working as expected. Sorry, I totally forgot to put what I wanted lol. Is there a cleaner way to test nowWeather and todayWeather against lists of strings other than using OR (||) everytime? For each possibility (i.e Hot, Wet, Cold) I want to test users input against several different lists. – UnclePete Oct 23 '20 at 16:01

1 Answers1

0

The logical OR || remains the best solution, unless you prefer things like ['hot', 'sunny', 'warm'].indexOf(nowWeather) > -1

Guerric P
  • 30,447
  • 6
  • 48
  • 86
  • I'm guessing it was probably because [this](https://stackoverflow.com/a/4728164) [isn't](https://stackoverflow.com/a/36861726) [new](https://stackoverflow.com/a/13737101). – Patrick Roberts Oct 23 '20 at 16:04
  • I thought about using that option but I think I'd prefer to stick with OR || if there isn't a cleaner way. Can I check users input against an array though? – UnclePete Oct 23 '20 at 16:04
  • Well yes my answer tells you how to do so. I don't get what you mean. – Guerric P Oct 23 '20 at 16:08