JavaScript More Complex Problems
JavaScript More Complex Problems
Julia's plan is to hang out with her friend Colt at the park.
The && symbol is the logical AND operator, and it is used to combine two logical expressions
into one larger logical expression. If both smaller expressions are true, then the entire expression
evaluates to true. If either one of the smaller expressions is false, then the whole logical
expression is false.
Another way to think about it is when the && operator is placed between the two statements, the
code literally reads, "if Colt is not busy AND the weather is nice, then go to the park".
Logical expressions
Logical expressions are similar to mathematical expressions, except logical expressions evaluate
to either true or false.
11 != 12
Returns: true
You’ve already seen logical expressions when you write comparisons. A comparison is just a
simple logical expression.
Similar to mathematical expressions that use +, -, *, / and %, there are logical operators &&, ||
and ! that you can use to create more complex logical expressions.
Logical operators
Logical operators can be used in conjunction with boolean values (true and false) to create
complex logical expressions.
By combining two boolean values together with a logical operator, you create a logical
expression that returns another boolean value. Here’s a table describing the different logical
operators:
By using logical operators, you can create more complex conditionals like Julia’s weekend
example.
Note - This video does not have an audio. It was created as a visual to aid learning.
Logical operators can be used to combine multiple conditional statements into a single statement.
TIP: Logical expressions are evaluated from left to right. Similar to mathematical expressions,
logical expressions can also use parentheses to signify parts of the expression that should be
evaluated first.
Truth tables
Before you advance any further in the lesson, here’s the truth tables for logical AND ( && ) and
logical OR ( || ).
Truth Tables
&& (AND)
A B A && B
Truth tables are used to represent the result of all the possible combinations of inputs in a
logical expression. A represents the boolean value on the left-side of the expression and B
represents the boolean value on the right-side of the expression.
Truth tables can be helpful for visualizing the different outcomes from a logical expression.
However, do you notice anything peculiar about the truth tables for logical AND and OR?
Short-circuiting
In some scenarios, the value of B in logical AND and OR doesn't matter.
In both tables, there are specific scenarios where regardless of the value of B, the value of A is
enough to satisfy the condition.
For example, if you look at A AND B, if A is false, then regardless of the value B, the total
expression will always evaluate to false because both A and B must be true in order for the
entire expression to be true.
This behavior is called short-circuiting because it describes the event when later arguments in a
logical expression are not considered because the first argument already satisfies the condition.
Directions:
For this quiz, you're going to help solve a fictitious murder mystery(opens in a new tab) that
happened here at Udacity! A murder mystery is a game typically played at parties wherein one of
the partygoers is secretly, and unknowingly, playing a murderer, and the other attendees must
determine who among them is the criminal. It's a classic case of whodunnit(opens in a new tab).
Since this might be your first time playing a murder mystery, we've simplified things quite a bit
to make it easier. Here's what we know! In this murder mystery there are:
four rooms: the ballroom, gallery, billiards room, and dining room,
four weapons: poison, a trophy, a pool stick, and a knife,
and four suspects: Mr. Parkes, Ms. Van Cleve, Mrs. Sparr, and Mr. Kalehoff.
And we know that each suspect was located in a specific room at the time of the murder.
Afterwards, use this template to print a message to the console if the mystery was solved:
What goes into the three blank spaces? You can fill in the blanks with the name of the suspect,
the room, and the weapon! For example, an output string may look like:
node murder-mystery.js
How will you know if your code works? Change the values of room and suspect and re-run the
code. If the case is solved, you should get the solution printed in the console, e.g. "Ms. Van
Cleve did it in the gallery with the trophy!".
If the case is not solved, you should get a message saying "The case is not solved!"
Expected Outcomes
room suspect Case is Solved?
Start Workspace
After starting your workspace, remember to keep the page open. Closing the page will shut down
the workspace and halt any associated processes.
Try the quiz on your own first. If you get stuck, you can check the solution by clicking the button
below.
Show/Hide Solution