tonight’s javascript topics

15
Tonight’s JavaScript Topics Conditional Statements: if and switch The Array Object Looping Statements: for, while and do- while

Upload: dima

Post on 22-Feb-2016

43 views

Category:

Documents


0 download

DESCRIPTION

Tonight’s JavaScript Topics. Conditional Statements: if and switch The Array Object Looping Statements: for, while and do-while. Conditional Statements in JavaScript. A conditional statement is a statement that runs a command or command block only when certain conditions are met. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Tonight’s JavaScript Topics

Tonight’s JavaScript Topics

• Conditional Statements: if and switch• The Array Object• Looping Statements: for, while and do-while

Page 2: Tonight’s JavaScript Topics

Conditional Statements in JavaScript• A conditional statement is a statement that

runs a command or command block only when certain conditions are met.

Page 3: Tonight’s JavaScript Topics

The if Statement in JavaScript

• To test a single condition, use: if (condition) {

commands }

• To test between two conditions, use: if (condition) {

commands if condition is true } else {

commands if otherwise }

Page 4: Tonight’s JavaScript Topics

The if Statement in JavaScript (cont.)• To test multiple conditions, use: if (condition 1) {

first command block } else if (condition 2) {

second command block } else if (condition 3) {

third command block } else {

default command block }

Page 5: Tonight’s JavaScript Topics

The Switch Statement in JavaScript• To test for different values of an expression, use:

switch (expression) { case label1: commands1 break; case label2: commands2 break; case label3: commands3 break;

... default: default commands

}• The break statement terminates any program loop or conditional.

Page 6: Tonight’s JavaScript Topics

A Complete Example

• Let’s modify last week’s dice program to play a simplified version of craps:– if the dice roll is 2, 3 or 12, the user loses (craps)– if the dice roll is 7 or 11, the user wins– any other number and the user continues rolling

• We’ll use if statements to do the job. As an outside-class exercise, try changing the program to use a switch statement.

Page 7: Tonight’s JavaScript Topics

The Array Object in JavaScript• An array is a collection of data values organized under a

single name– Each individual data value is identified by an index

• To create an array: var array = new Array(length);

• To populate an element of the array: array[i] = value;

• To create and populate an array: var array = new Array(values);

• To get the size of the array, use the property array.length

Page 8: Tonight’s JavaScript Topics

Some Useful Array Methods

Page 9: Tonight’s JavaScript Topics

A Program Example Using an Array

• Let’s write a JavaScript program that does the following:– displays a button on the web page– whenever the user presses the button, a random

fortune will be displayed to the user.– the list of “random” fortunes will be stored in an

array

Page 10: Tonight’s JavaScript Topics

Looping in JavaScript: The for Loop

• A program loop is a set of commands executed repeatedly until a stopping condition has been met

• for loops use a counter variable to track the number of times a block of commands is run

• To create a for loop, use:for (start at; continue until; update by){

commands to repeat}

Page 11: Tonight’s JavaScript Topics

A for Loop Example

Page 12: Tonight’s JavaScript Topics

Another for Loop Example

• for loops are often used to cycle through the different values contained within an array:

Page 13: Tonight’s JavaScript Topics

Loops in JavaScript: The while Loop• A while loop runs as long as a specific condition

is true• To create a while loop, use: while(continue until condition){

commands to repeat }

• Example: var sum = 0; while(sum < 10) {

sum = sum + Math.random(); }

Page 14: Tonight’s JavaScript Topics

Loops in JavaScript: The do-while Loop

• A do-while loop runs as long as a specific condition is true but the condition is at the end

• To create a do-while loop, use: do {

commands to repeat while(continue until condition); • A do-while loop is always executed at least one

time which isn’t necessarily true of while loops.• Watch out for infinite loops!

Page 15: Tonight’s JavaScript Topics

A Final Example: Rock, Paper, Scissors

1. For player 1, generate a random # between 1 and 3. 1= rock, 2=paper, 3-scissors.  2. For player 2, generate a random # between 1 and 3. 1= rock, 2=paper, 3-scissors. 3. Figure out which player wins by the rules:

- paper covers rock (paper wins) - scissors cuts paper (scissors win)

- rock breaks scissors (rock wins) - both players have the same item (tie) 

4. Display an alert message like: “Player 1 has rock. Player 2 has paper. Player 2 wins.”  5. Keep playing if they tie until one player wins.