Skip to content

Latest commit

 

History

History
133 lines (93 loc) · 4.82 KB

2015-05-26-js-week2.md

File metadata and controls

133 lines (93 loc) · 4.82 KB

Statements & Loops

Review

Students volunteer to do the following either on the whiteboard or projector, depending on complexity:

  1. Declare a variable and then update its value.
  2. Declare a string variable, then add to it by concatenating another string.
  3. Create two number variables and multiply them together, storing the result in a sum variable.
  4. Create a simple object and retrieve some data from it, either via bracket or dot notation.

Class Workflow

Explanation of context, importance, reasoning for order

  1. if, else, else if
  2. switch
  3. for
  4. while

If Statements

An if statement executes a statement if its condition evaluates to true, and provides an else statement for when the condition is false. As we talked about last week, this condition is evaluating to a boolean value of either true or false.

var x = 0;
var y = 10;

if (x > y){
	alert(x + " is greater than " + y);
else {
	alert(y + " is greater than " + x);
}

else if

If there are more than two conditions to be evaluated, additional if...else if statements can be included:

var x = 0;
var y = 10;

if (x > y){
	alert(x + " is greater than " + y);
else if (x == y){
	alert(x + " is equal to " + y);
}
else {
	alert(y + " is greater than " + x);
}

The greater than, less than, and double equals signs are called boolean comparison operators. Here is a comprehensive list of all available boolean comparison operators.

You can also check if more than one condition is true, if a condition is not true, or if one of many conditions is true:

//both true

Switch Statements

A switch statement evaluates an expression compared to a defined set of cases. If it finds a match, the statements associated with that case will be executed. The break after each tells the program to kick out of the switch statement after the code for a matching case has been executed. If the break is omitted, the program will execute statements for all the following cases as well, regardless of whether or not they match.

Let's try writing one for days of the week, alerting a different message

var _day = "Tuesday";
switch (_day) {
	case "Monday":
    	alert("Today is Monday");
        break;
    case "Tuesday":
    	alert("Today is Tuesday");
        break;
    case "Wednesday":
    	alert("Today is Wednesday");
        break;
    case "Thursday":
    	alert("Today is Thursday");
        break;
    case "Friday":
    	alert("Today is Friday");
        break;
    case "Saturday":
    	alert("Today is Saturday");
        break;
    case "Sunday":
    	alert("Today is Sunday");
        break;
    default:
    	alert("That is not a day of the week");
}

While Loops

A while loop continuously executes statements until the condition it's evaluating is no longer true. There must be some increment specified within the statement, otherwise you will have an infinite loop and your program will crash.

var _countdown = 10;
var i = 0;
while (_countdown > i) {
	alert('Time left: ' + _countdown);
    _countdown -= 1;
}

For Loops

A for loop is effectively a special kind of while loop. The key differences are semantic:

  1. A for loop can have a counter variable, condition to evaluate, and the increment in one line, which helps readability.
  2. The counter variable can be declared within the for loop, keeping it out of the global namespace.
  3. While loops are technically indefinite at the time of compiling (though if written properly will at one point stop), whereas for loops have a finite number of iterations defined from the start.

Let's create a for loop that iterates through all the colors of the rainbow and outputs a message listing them:

var _rainbow = ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet'];
var _message = "The colors of the rainbow are ";
for (var i=0; i < _rainbow.length; i++) {
	_message = _message + ', ' + _rainbow[i];
}
alert(_message);

Resources

http://stackoverflow.com/questions/3875114/why-use-a-for-loop-instead-of-a-while-loop

Homework

poker hand

Create an object that contains three poker hands: a Full House, a Royal Flush, and 4 of a Kind. The key should be the name of the hand, and the value an array of the five cards (using strings, i.e. "King of Clubs", "Two of Diamonds", etct).

Create a variable called "hand" and use a switch statement to evaluate the hand and alert some information accordingly.

In each case, use a for loop to create a string that reads "This hand is a Hand Name and contains a Card 1, Card 2, Card 3, Card 4, Card 5".