JavaScript Exercise Logical Operators (doesn't work)

javascript logical operators
javascript or operator
javascript comparison operators
ternary operator javascript
not operator
javascript
javascript if and
logical operators java

I'm doing a JavaScript course and there's a exercise about logical operators that's doesn't work right. The question is:

Using the logical operators you just learned, write an expression that represents the following statement: "I would like an ice cream with strawberry and chocolate or stracciatella, or an ice cream of any flavour but caramel."

Than I've wrote those two script versions but any of that is right according the course:

VERSION 1:

var chocolate;
var strawberry;
var stracciatella;
var caramel;
var iceCream = ((strawberry && stracciatella != (caramel)) || (strawberry && chocolate != (caramel)) || (chocolate && stracciatella != (caramel)));

VERSION 2:

var chocolate;
var strawberry;
var stracciatella;
var caramel;
var iceCream = ((strawberry && stracciatella) || (strawberry && chocolate) || (chocolate && stracciatella) != (caramel));

I really don't know whats going wrong buts. Maybe I didn't interpreted the question right, so if someone in community help me to solve this I'll be very thankful.

Precedence with logical operators:

  1. NOT: !
  2. Comparison Operators: ===, >, <, >=, <=
  3. AND: &&
  4. OR: ||

If we apply this precedence to the statment "with strawberry and chocolate or stracciatella", then iceCream should have this assignment.

var iceCream = ((strawberry && chocolate) || stracciatella) || !caramel;

But the answer is not correct. The hint is...

Did you consider strawberry and stracciatella?

JavaScript Comparison and Logical Operators, The OR || operator does the following: Evaluates operands from left to right. For each operand, converts it to boolean. If the result is true , stops� I'm doing a JavaScript course and there's a exercise about logical operators that's doesn't work right. The question is: Using the logical operators you just learned, write an expression that represents the following statement: "I would like an ice cream with strawberry and chocolate or stracciatella, or an ice cream of any flavour but caramel."

Here's an example that returns true based on what you've pipped. But if you wanted I could show you a function that will let only the perfumes of ice wanted.

const iceFlavour = ['chocolate', 'strawberry', 'stracciatella','caramel']

var iceCream = (((iceFlavour[1] && iceFlavour[2]) !== 'caramel') ||
                ((iceFlavour[0] && iceFlavour[1]) !== 'caramel') ||
               (iceFlavour[0] || iceFlavour[1] || iceFlavour[2] !== 'caramel'))

console.log(iceCream)

Logical operators, In this tutorial, you will learn how to use JavaScript logical operators including In this case, b is false , the && operator doesn't need to evaluate the second� JavaScript logical operators covering description, example code, output of example, online practice editor and explanation by w3resource.com

Firstly you are saying "!= caramel" which is not a logical operator but a comparison operator so "!= caramel" will not work in the context of the task.

It should be "&& !caramel".

Also, you have not assigned any value to your variables which will make the expression result in "undefined"; Try giving each of them a value like "let chocolate = 'chocolate'" or a boolean like "let chocolate = true" and "let caramel = false".

Hope that can give you some direction.

You can read more about logical operators here:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators

An Introduction to JavaScript Logical Operators By Examples, JavaScript logical operators covering description, example code, output of example, online practice editor and explanation by w3resource.com. There are three logical operators in JavaScript: || (OR), && (AND), ! (NOT). Although they are called “logical”, they can be applied to values of any type, not only boolean. Their result can also be of any type. Let’s see the details. || (OR) The “OR” operator is represented with two vertical line symbols:

JavaScript : Logical Operators - AND OR NOT, Though JavaScript allows this practice, you should avoid it in most cases. JavaScript can coerce any value to a boolean value, and that's what it does in� Operator Description == equal to === equal value and equal type!= not equal!== not equal value or not equal type > greater than < less than >= greater than or equal to <= less than or equal to? ternary operator

JavaScript Programming Language: comparisons & conditionals, In this tutorial, we will also explain what is a ternary operator JavaScript coders use and what purpose does it serve. Contents. 1. JavaScript� Likewise, with the OR operator, if the first condition is true then it doesn't bother to look at the second condition. Logical operators can simplify your program by eliminating the need for nested ifs, in some cases. Let's revisit that Loan Qualifier program, we did and solve it using logical operators instead of the nested ifs.

Tutorial on JavaScript Logical Operators such as Or in Javascript, Learn and Understand the JavaScript Logical Operators: AND (&&), OR indicate any value — it simply doesn't matter what it is as JavaScript� Exercises. We have gathered a variety of JavaScript exercises (with answers) for each JavaScript Chapter. Try to solve an exercise by editing some code, or show the answer to see what you've done wrong. Count Your Score. You will get 1 point for each correct answer. Your score and total score will always be displayed.

Learn JavaScript: Logical AND / OR / NOT, Master the Power Behind Javascript's Logical Operators If the Javascript engine doesn't have to evaluate the right side of the expression, it won't. For example: if(x === 1 || x Let's try some practice problems. See if you can� JavaScript Exercises, Practice, Solution: JavaScript is a cross-platform, object-oriented scripting language. Inside a host environment, JavaScript can be connected to the objects of its environment to provide programmatic control over them.

Comments
  • Your variables have no values?
  • All of your flavor variables are undefined.
  • First: use spaces. Second: how does the test case look like? Why all your variables are undefined?
  • What I suspect you're supposed to do is write a function that accepts those flavors as boolean arguments, and THEN your expression at the end will return true or false appropriately
  • @T.J.Crowder sorry, yes I didn't mean to imply that the expression is literally the correct answer, just that having the context of a function with arguments will make the expression at least make sense, and will then be open for adjustment to match the criteria.
  • ita was wrong because I've tried as && !caramel and the correct is || !caramel. Thanks Tom! var chocolate = true; var strawberry = true; var stracciatella = true; var caramel = false; var iceCream = ((strawberry && chocolate) || (strawberry && stracciatella) || !caramel);