Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
8 views

JavaScript More Complex Problems

Uploaded by

Tesfaye
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

JavaScript More Complex Problems

Uploaded by

Tesfaye
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

JavaScript

3.8 More Complex Problems


Problems Aren't Always Simple
With most problems there's more things to consider before you can actually solve the problem.

Example: Deciding what to do this weekend

Julia's plan is to hang out with her friend Colt at the park.

For Julia's plans to work:

1. Colt must be available


2. The weather must be nice

In JavaScript we can represent complex problems by combining logical expressions with


special Logical Operators

Here’s the logical expression used to represent Julia’s weekend plans:

var colt = "not busy";


var weather = "nice";

if (colt === "not busy" && weather === "nice") {


console.log("go to the park");
}

Prints: "go to the park"

Notice the && in the code above.

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:

Operator Meaning Example How it works

Logical value1 &&


&&
value2
Returns true if both value1 and value2 evaluate to true.
AND

value1 || Returns true if either value1 or value2 (or even both!)


|| Logical OR value2
evaluates to true.

Returns the opposite of value1. If value1 is true, then !


! Logical NOT !value1
value1 is false.

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.

operators called logical operators.


Learn More
Read the documentation: MDN Web Docs - Logical Operators
Logical AND and OR

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

true true true

true false false

false true false

false false false


|| (OR)
A B A || B

true true true

true false true

false true true

false false false

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.

Quiz: Murder Mystery

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.

We also know that each weapon corresponds to a particular room, so...

 the poison belongs to the ballroom,


 the trophy belongs to the gallery,
 the pool stick belongs to the billiards room,
 and the knife belongs to the dining room.

And we know that each suspect was located in a specific room at the time of the murder.

 Mr. Parkes was located in the dining room.


 Ms. Van Cleve was located in the gallery.
 Mrs. Sparr was located in the billiards room.
 Mr. Kalehoff was located in the ballroom.

To help solve this mystery, write a combination of conditional statements that:

1. sets the value of weapon based on the room and


2. sets the value of solved to true if the value of room matches the suspect's room

Afterwards, use this template to print a message to the console if the mystery was solved:

__________ did it in the __________ with the __________!

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:

Mr. Parkes did it in the dining room with the knife!

Running Your Code

Enter the following in the terminal:

node murder-mystery.js

Testing Your Code

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?

dining room Mr. Parkes Yes

gallery Ms. Van Cleve Yes

billiards room Mrs. Sparr Yes

ballroom Mr. Kalehoff Yes

billiards room Ms. Van Cleve No

dining room Mrs. Sparr No

gallery Mr. Kalehoff No

ballroom Mr. Parkes No

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

You might also like