If...else statements
If...else statementsallow you to execute certain pieces of code based on a condition, or set of conditions, being met.
if (/* this expression is true */) {
// run this code
} else {
// run this code
}
This is extremely helpful because it allows you to choose which piece of code you want to run based on the result of an expression. For example,
var a = 1;
var b = 2;
if (a > b) {
console.log("a is greater than b");
} else {
console.log("a is less than or equal to b");
}
Prints:"a is less than or equal to b"
A couple of important things to notice aboutif...else
statements.
The value inside theif
statement is always_converted_to true or false. Depending on the value, the code inside theif
statement is run or the code inside theelse
statement is run, but not both. The code inside theif
andelse
statements are surrounded bycurly braces{...}
to separate the conditions and indicate which code should be run.
TIP:When coding, sometimes you may_only_want to use an
if
statement. However, if you try to use only anelse
statement, then you will receive the errorSyntaxError: Unexpected token else
. You’ll see this error becauseelse
statements need anif
statement in order to work. You can’t have anelse
statement without first having anif
statement.
Else if statements
In JavaScript, you can represent this secondary check by using an extra if statement called anelse if statement.
var weather = "sunny";
if (weather === "snow") {
console.log("Bring a coat.");
} else if (weather === "rain") {
console.log("Bring a rain jacket.");
} else {
console.log("Wear what you have on.");
}
Prints:Wear what you have on.
By adding the extraelse if
statement, you're adding an extra conditional statement.
If it’s not going to snow, then the code will jump to theelse if
statement to see if it’s going to rain. If it’s not going to rain, then the code will jump to theelse
statement.
Theelse
statement essentially acts as the "default" condition in case all the otherif
statements are false.
Murder Mystery:
For this quiz, you're going to help solve a fictitious murder mysterythat 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.
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 theballroom
,- the
trophy
belongs to thegallery
,- the
pool stick
belongs to thebilliards room
,- and the
knife
belongs to thedining room
.And we know that each suspect was located in a specific room at the time of the murder.
Mr. Parkes
was located in thedining room
.Ms. Van Cleve
was located in thegallery
.Mrs. Sparr
was located in thebilliards room
.Mr. Kalehoff
was located in theballroom
.To help solve this mystery, write a combination of conditional statements that:
- sets the value of
weapon
based on theroom
and- sets the value of
solved
totrue
if the value ofroom
matches thesuspect
's roomAfterwards, print the following to the console if the mystery was solved:
__________ did it in the __________ with the __________!
Fill in the blanks with the name of the suspect, the room, and the weapon. For example,
Mr. Parkes did it in the dining room with the knife!
TIP:Make sure to test your code with different values. For example,
If
room
equalsgallery
andsuspect
equalsMs. Van Cleve
, thenMs. Van Cleve did it in the gallery with the trophy!
should be printed to the console.
/*
* Programming Quiz: Murder Mystery (3-4)
*/
// change the value of `room` and `suspect` to test your code
var room = "dining room";
var suspect = "Mr. Parkes";
var weapon = "";
var solved = false;
if (room === "dining room" && suspect === "Mr. Parkes") {
weapon = "knife";
solved = true;
} else if (room === "ballroom" && suspect === "Mr. Kalehoff") {
weapon = "poison";
solved = true;
} else if (room === "gallery" && suspect === "Ms. Van Cleve") {
weapon = "trophy";
solved = true;
} else if (room === "billiards room" && suspect === "Mrs. Sparr") {
weapon = "pool stick";
solved = true;
} else {
console.log("unsolved");
}
if (solved) {
console.log(suspect + ' did it in the ' + room + ' with the ' + weapon + '!');
}