0

I'm trying to understand how logical operators can be combined in Javascript. For instance, how will i print out a statement if the following conditions are true

  1. if flavor is set to vanilla or chocolate and
  2. if vessel is set to cone or bowl and
  3. if toppings is set to sprinkles or peanuts

This is the code i have written but comparison is false, it still prints out a message

var flavor = 'vanilla';
var vessel = 'cup';
var toppings = 'peanuts';

if (flavor === 'vanilla' || 'chocolate' && vessel === 'cone' || 'bowl') {
   if (toppings === 'peanuts' || 'sprinkles') {
       console.log('I\'d like two scoops of ' + flavor + ' ice cream in a ' + vessel + ' with ' + toppings + '.');
   }
}

Result - I'd like two scoops of vanilla ice cream in a plate with peanuts.

What am I missing? Can't seem to see where the error is coming from.

Mena
  • 1,873
  • 6
  • 37
  • 77

1 Answers1

2

This:

flavor === 'vanilla' || 'chocolate'

is invalid. You must test each condition independently:

(flavor === 'vanilla' || flavor === 'chocolate')

The others have similar errors.

Randy Casburn
  • 13,840
  • 1
  • 16
  • 31
  • Yeah, that fixed it. Now i understand how the logical operators work better. – Mena May 12 '18 at 00:18
  • 1
    I realized i could have all 3 conditions in one statement too like this `if ((flavor === 'vanilla' || flavor === 'chocolate') && (vessel === 'cone' || vessel === 'bowl') && (toppings === 'peanuts' || toppings === 'sprinkles')) { console.log('I\'d like two scoops of ' + flavor + ' ice cream in a ' + vessel + ' with ' + toppings + '.'); }` – Mena May 12 '18 at 00:26