1

I started an intro to JavaScript course, and we're going over logical operators. The goal of my script is to print a statement if a few conditions are met.

I have 3 variables (x, y, z for example) and I need it to print to console if x = a || b AND y = c || d AND z = e || f.

My code is:

var flavor = "strawberry";
var vessel = "cone";
var toppings = "cookies";


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

It needs to have vanilla or chocolate && cone or bowl && sprinkles or peanuts to be true to print. With my code, it prints whatever values are in the variables, no matter what they are.

Is there some syntax wrong with my code? Or can you not compare that many things in one statement? As I said, it's an intro course, so I can't imagine it would be all that complex to start. Something just isn't firing in my brain. lol

Any help/explanations would be greatly appreciated.

Thanks in advance!!

4
  • 3
    flavor === "vanilla" || "chocolate" -> flavor === "vanilla" || flavor === "chocolate" and repeat for the rest of the cases. You might want a helper function to make your code more readable. Commented Oct 17, 2018 at 22:30
  • FYI, what's really happening is if ((flavor === "vanilla") || (true && (vessel === "cone")) || (true && (toppings === "sprinkles")) || true) which as you can see will always be true. Commented Oct 17, 2018 at 22:38
  • Possible duplicate of Compare multiple values against the same variable Commented Oct 17, 2018 at 22:40
  • This worked up until the last comparison. No matter what I change toppings to, it always prints the statement. Commented Oct 17, 2018 at 22:49

2 Answers 2

5

The problem is how you're using the OR conditions. In JS when you use an object different than undefined or null or 0, or, "", or NaN the condition returns true.

So, you need to change that. Basically, if you need to compare multiple times with the same variable, do the following:

var flavor = "strawberry";
var vessel = "cone";
var toppings = "cookies";

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

Or, you can use arrays along with the function includes.

var flavor = "strawberry";
var vessel = "cone";
var toppings = "cookies";

if (["vanilla", "chocolate"].includes(flavor) && ["cone", "bowl"].includes(vessel) && ["sprinkles", "peanuts"].includes(toppings)) {
  console.log("I'd like two scoops of " + flavor + "ice cream in a " + vessel + "with  " + toppings + ".");
} else {
  console.log("No ice cream for you.");
}

Sign up to request clarification or add additional context in comments.

3 Comments

Also don't forget "", NaN, and obviously false in that list.
I updated my code using the formatting above, and the (( )) throws an error. I used [] around each set of arguments and it worked... but it's printing no matter what the last variable is, even if it isn't = to the variable.
Got it!! I accidentally had one too many! Thanks for the help. It's good to know I can group each comparison in () and it works.
1

There are rules describing how you can chain together multiple comparisons.

Those rules are known as precedence rules, but it is usually easier to use extra parentheses to group comparisons together so that you don't have to worry about the precedence rules as much. Here is your if statement with the correct parentheses:

if ((flavor === "vanilla" || flavor === "chocolate") && (vessel === "cone" || vessel === "bowl") && (toppings === "sprinkles" || toppings === "peanuts"))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.