javascript iteration
play

Javascript: Iteration ATLS 3020 - Digital Media 2 Week 4 - Day 1 - PowerPoint PPT Presentation

Javascript: Iteration ATLS 3020 - Digital Media 2 Week 4 - Day 1 Repeating Code Exercise: Have the user enter a number between 1 and 10. Print the string Hello that many times - Programmers are lazy - Never copy and paste code (there


  1. Javascript: Iteration ATLS 3020 - Digital Media 2 Week 4 - Day 1

  2. Repeating Code Exercise: Have the user enter a number between 1 and 10. Print the string “Hello” that many times - Programmers are lazy - Never copy and paste code (there is an easier way to do it)

  3. For Loops Javascript for(initialization; condition; update) There are always 3 parts to the condition { // inside of for loop Initialization var i=0 } - start counting at 0 // outside of for loop - i = index Condition i<10 Syntax is important! - keep looping as long as i is less than 10 - we do not go to 10, we go until i is just less than ALL for loops must come in this form: 10 for(initialization, condition, update) { Update i++ //stuff - at the end of every loop, we add 1 to i }

  4. For Loops Start var i i = 0 i = 1 i = 2 Javascript document.write("Start"); 0 1 2 alert(i) alert(i) alert(i) for(var i=0; i<3; i++) { alert(i); } i++ i++ i++ document.write("End"); End i < 3 i < 3 i < 3

  5. Exercises i++ i = i + 1 i+=2 i = i + 2 Print the numbers 0-4 Print the odd numbers between 1-10 Javascript Javascript for(var i=0; i<5; i++) for(var i=1; i<11; i+=2) { { document.write("Index: " + i + "<br/>"); document.write("Index: " + i + "<br/>"); } } Print the colors red, blue, green, yellow User enters 5 colors, print the array Javascript Javascript var colors = []; var colors = ["red", "blue", "green", "yellow"]; alert("Enter 5 Colors."); for(var i=0; i<4; i++) for(var i=0; i<5; i++) { { document.write(colors[i] + "<br/>"); colors.push(prompt("Enter a color")); } } document.write(colors);

  6. While Loops Javascript while(condition) The condition must always be true { // inside of while loop The while loop will keep running until the condition is } false // outside of while loop True conditions False conditions Syntax is the same as a for loop while(true) while(false) ALL for loops must come in this form: var i = 1; var i = 1; while(i == 1) while(i == 0) for(condition) { //stuff }

  7. Exercises Keep asking user to enter a color Print the numbers 0-4 until they enter “red” Javascript Javascript var i = 0; while(i < 5) var color; { while(color != "red") document.write("Index: " + i + "<br/>"); { i++; color = prompt("Enter a color"); } } Keep asking user to enter a color Keep asking user to enter a number less until they enter “red” and “blue” than 5 or greater than 10 Javascript Javascript var color1, color2; var number; while(color1 != "red" && color2 != "blue") while(number < 5 || number > 10) { { color1 = prompt("Enter a color"); number = prompt("Enter a number"); color2 = prompt("Enter another color"); } }

  8. The game of War Where can we use a for loop? - Dealing the cards out to the two players - Playing the game (exact number of rounds) Where can we use a while loop? - Dealing the cards out to the two players - Playing the game (exact number of rounds)

  9. Lab 5 Continue playing the card game War ! 1. Download the starter code from the class website 2. Write a for loop that will play 20 rounds of the game. 2.5 Play a round: (This step is the same as in Lab 3) Remove the first cards from both of the player’s decks. Compare the two cards together. Print each of the cards and print who won the round, then add the two cards to the winner’s deck. (If it is war, print that the players are going to war and add both cards to player 1’s deck). 3. Count how many cards each player has. Print the winner as the player with the most number of cards 4. (Extra credit) Write a while loop that will keep going until one of the players is out of cards. 4.5 Play a round of the game (same as above).

Recommend


More recommend