Multiple operators in an if statement?


#1

So I've got the below code and it's not finished yet, but I'm attempting to run through it testing all the if/else if scenarios of case = 'coffee' and it's just not working as I would expect. The code runs fine, but the expected result is wrong.

For example if I input 'coffee' and then 'no' and then 'study',
I would expect this answer: "You chose not to drink more coffee... sleep came and destroyed you and your passing grade."
But instead I get this one: "Thankfully you drank enough coffee and it helped a lot! You managed to stay up studying and you feel adequately prepared to take your final and pass the class."

Is something in my syntax incorrect, am I not allowed to use multiple operators in an if statement, or am I doing something else wrong?

var user = prompt("You've got finals tomorrow morning, but you've barely started studying. Do you choose to drink coffee and cram all night or go to bed and try to copy off your classmate tomorrow? {Choose: coffee or bed}").toLowerCase();

switch(user){
   case 'coffee':
    var caffeineEffect = prompt("Does caffeine get you wired easily? {Yes or No}").toLowerCase();
    var caffeineChoice = prompt("Coffee doesn't seem to be kicking in. Drink more or try to study?").toLowerCase();

    if (caffeineEffect === 'yes' && (caffeineChoice === 'drink more'||'more'||'drink')){
        console.log("You drank some more coffee, but then got too wired! You were jittery all night and couldn't focus on studying. Now you've crashed just before your exam and missed it completely...");
    } else if (caffeineEffect === 'yes' && (caffeineChoice === 'try to study'||'study'||'try studying')){
          console.log("Thankfully that caffeine did end up kicking in. You managed to stay up studying and you feel adequately prepared to take your final and pass the class.");
    } else if (caffeineEffect === 'no' && (caffeineChoice === 'drink more'||'more'||'drink')){
          console.log("Thankfully you drank enough coffee and it helped a lot! You managed to stay up studying and you feel adequately prepared to take your final and pass the class.");
    } else console.log("You chose not to drink more coffee... sleep came and destroyed you and your passing grade.");
      break;

   default:
    console.log("You can only choose 'coffee' or 'bed'!");
  }

Thanks in advance for the help!


#2

Expression logic is not distributive, but linear and governed by order or precedence. In math we call this order of operations. That means we cannot write a relationship in one operand and expect it to carry-over to the next. It won't.

(caffeineChoice === 'drink more'||'more'||'drink')

If the choice is not 'drink more', it won't matter. The net yield is true. Strings with length greater than zero (not the empty string) are truthy, so yield a Boolean true.

The JavaScript way to write this expression would be,

// Let c = caffeineChoice
// for brevity

c === 'drink more' || c === 'drink' || c === 'more'

Get what I mean?


#3

That was really helpful/informative. Thank you!