Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Tonight’s JavaScript Topics 1 Conditional Statements: if and switch The Array Object Looping Statements: for, while and do-while."— Presentation transcript:

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

2 Conditional Statements in JavaScript 2 A conditional statement is a statement that runs a command or command block only when certain conditions are met.

3 The if Statement in JavaScript 3 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 }

4 The if Statement in JavaScript (cont.) 4 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 }

5 The Switch Statement in JavaScript 5 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.

6 A Complete Example A Complete Example 6 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.

7 The Array Object in JavaScript 7 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

8 Some Useful Array Methods Some Useful Array Methods 8

9 A Program Example Using an Array A Program Example Using an Array 9 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

10 Looping in JavaScript: The for Loop 10 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 }

11 A for Loop Example A for Loop Example 11

12 Another for Loop Example Another for Loop Example 12 for loops are often used to cycle through the different values contained within an array:

13 Loops in JavaScript: The while Loop 13 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(); }

14 Loops in JavaScript: The do-while Loop 14 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!

15 A Final Example: Rock, Paper, Scissors 15 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.


Download ppt "Tonight’s JavaScript Topics 1 Conditional Statements: if and switch The Array Object Looping Statements: for, while and do-while."

Similar presentations


Ads by Google