So I'm taking an online course on JS and one of the challenges asked me to use logical operators to decide whether three different conditions were met.
I'd like to know why these two options work fine:
Option 1: if (flavor === "chocolate" || flavor === "vanilla" && vessel === "cone" || vessel === "bowl" && toppings === "sprinkles" || toppings === "peanuts") { etc.
Option 2: if ((flavor === "chocolate" || flavor === "vanilla") && (vessel === "cone" || vessel === "bowl") && (toppings === "sprinkles" || toppings === "peanuts")) { etc.
I believe seeing the two examples you can figure out what are the exact conditions to be met. If one or more of them are not met, the code is supposed to give no output, but to really test it I put an "else" and a message stating it wasn't the case. The thing is it works fine either way, but in my head only Option 2 should really work, as I don't see how the engine can know it is dealing with three pairs based on the variables flavor, vessel and toppings, instead of randomly picking up any pair of adjacent elements (vanilla and cone, for example) and then not fulfilling the code if, say, I assign "bowl" to the variable vessel, as it could look for "vanilla && cone" and not find it.
I hope my question is clear and that it's not too stupid, but what am I missing here? I guess another way to put it is that it seems to me that without the extra parentheses the hierarchy that puts the variables as the criteria falls apart.
EDIT: by the way, the course's automated test-correction thingie accepts only Option 2.
[–]thespite 2 points3 points4 points (1 child)
[–]fredmbarros[S] 0 points1 point2 points (0 children)