1 / 15

Tonight’s JavaScript Topics

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.

dima
Download Presentation

Tonight’s JavaScript Topics

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Conditional Statements in JavaScript 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 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.) 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 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 • 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 • 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: vararray = new Array(length); • To populate an element of the array: array[i] = value; • To create and populate an array: vararray = new Array(values); • To get the size of the array, use the property array.length

  8. Some Useful Array Methods

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

  10. 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 variableto track the number of times a block of commands is run To create a forloop, use: for (start at; continue until; update by){ commands to repeat }

  11. A for Loop Example

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

  13. Loops in JavaScript: The while Loop A while loop runs as long as a specific condition is true To create a whileloop, 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 A do-whileloop runs as long as a specific condition is true but the condition is at the end To create a do-whileloop, 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 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.

More Related