1 / 14

B118 Web Programming

B118 Web Programming. Session #7 More JavaScript Application Examples March 15, 2004. Tonight’s Agenda. Midterm #1 Discussion Chapters 3 & 7 quiz tonight !!! Chapters 10, 11, 14 quiz next week !!! JavaScript Arrays Advanced JavaScript Control Structures. JavaScript Arrays.

wyome
Download Presentation

B118 Web Programming

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. B118 Web Programming Session #7 More JavaScript Application Examples March 15, 2004

  2. Tonight’s Agenda • Midterm #1 Discussion • Chapters 3 & 7 quiz tonight !!! • Chapters 10, 11, 14 quiz next week !!! • JavaScript Arrays • Advanced JavaScript Control Structures

  3. JavaScript Arrays

  4. JavaScript Examples • Twelve Days of Christmas • Using Switch statement • Using Arrays • Using Objects 9.11 (“The Twelve Days of Christmas” Song) Write a script that uses repetition and switch structures to print the song “The Twelve Days of Christmas.” One switch structure should be used to print the day (i.e., “First,” “Second,” etc.). A separate switch structure should be used to print the remainder of each verse.

  5. Twelve Days of Christmas Using Switch for( var i = 1; i <= 12; i++ ){ var day, newGift, gifts, oldGifts; switch( i ){ case 1: day = "first"; newGift = "a partridge in a pear tree."; oldGifts = "and " + newGift; break; case 2: day = "second"; newGift = "two turtle doves"; break; } if ( i == 1 ) gifts = newGift; else{ gifts = newGift + "<br />" + oldGifts; oldGifts = gifts; } document.writeln("<br /><br />On the " + day + " of Christmas<br />" + "my true love sent to me:<br />" + gifts );

  6. Twelve Days of Christmas Using Arrays var gift_array = [ ["zero","gift opening time!!!"] , ["first","a partridge in a pear tree."] , ["second","two turtle doves"] ]; for( var i = 1; i <= 12; i++ ){ var day, newGift, gifts, oldGifts; day = gift_array[i][0]; newGift = gift_array[i][1]; if( i == 1 ) { gifts = newGift; oldGifts = "and " + newGift; } else{ gifts = newGift + "<br />" + oldGifts; oldGifts = gifts; } document.writeln( "<br /><br />On the " + day + " of Christmas<br />" + "my true love sent to me:<br />" + gifts ); }

  7. Twelve Days of Christmas Using Objects function gift_object(day,gift) { this.day = day this.gift = gift } var gift_object_array = [ new gift_object("zero","gift opening time!!!") , new gift_object("first","a partridge in a pear tree.") , new gift_object("second","two turtle doves") ]; for( var i = 1; i <= 12; i++ ){ var day, newGift, gifts, oldGifts; day = gift_object_array[i].day; newGift = gift_object_array[i].gift; if( i == 1 ) { gifts = newGift; oldGifts = "and " + newGift; } else{ gifts = newGift + "<br />" + oldGifts; oldGifts = gifts; } document.writeln("<br /><br />On the " + day + " of Christmas<br />" + "my true love sent to me:<br />" + gifts ); }

  8. Comparison • Switch • Reduces clutter from nested IF • Arrays • Groups all of the text into one area (easier to maintain/translate into another language) • Objects • Makes data structure internally managed (within the object)

  9. Advanced JavaScript Control Structures • For/in • Switch • ?:

  10. For/in • foreach elementinan array // sum all of the elements of theArray // (result is total = 15 at end of for loop var theArray = {1,2,3,4,5}; var total = 0; for (var element in theArray) total += theArray[ element ];

  11. Switch • Used instead of multiple, nested IF statements to handle situations where you have multiple selections • Example: Twelve Days of Christmas

  12. Switch Example • Nested IF choice = window.prompt( "Select a list style:\n" + "1 (bullet), 2 (numbered), 3 (lettered)", "1" ); if ( choice == 1 ) { listType = "Bullet List“} else { if ( choise == 2 ) { listType = "Ordered List: Numbered“ } else { if (choice == 3 ) { listType = "Ordered List: Lettered“ } else { validInput = false } } } }

  13. Switch Example • Replace with choice = window.prompt( "Select a list style:\n" + "1 (bullet), 2 (numbered), 3 (lettered)", "1" ); switch ( choice ) { case "1": listType = "Bullet List"; break; case "2": listType = "Ordered List: Numbered"; break; case "3": listType = "Ordered List: Lettered"; break; default: validInput = false; }

  14. ?: • Conditional operator • Instead of • Use if ( studentGrade >= 60 ) { document.writeln (“passed”) } else { document.writeln (“failed”) } } document.writeln ( studentGrade >= 60 ? “passed” : “failed” );

More Related