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...elsestatements.

The value inside theifstatement is always_converted_to true or false. Depending on the value, the code inside theifstatement is run or the code inside theelsestatement is run, but not both. The code inside theifandelsestatements 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 anifstatement. However, if you try to use only anelsestatement, then you will receive the errorSyntaxError: Unexpected token else. You’ll see this error becauseelsestatements need anifstatement in order to work. You can’t have anelsestatement without first having anifstatement.

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 ifstatement, you're adding an extra conditional statement.

If it’s not going to snow, then the code will jump to theelse ifstatement to see if it’s going to rain. If it’s not going to rain, then the code will jump to theelsestatement.

Theelsestatement essentially acts as the "default" condition in case all the otherifstatements 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...

  • thepoison 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, 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,

IfroomequalsgalleryandsuspectequalsMs. 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 + '!');
 }

results matching ""

    No results matching ""