Your SlideShare is downloading. ×
JavaScript Missing Manual, Chapter 3, part 2
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

JavaScript Missing Manual, Chapter 3, part 2

856
views

Published on

JavaScript Missing Manual, Chapter 3, part 2

JavaScript Missing Manual, Chapter 3, part 2

Published in: Technology

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
856
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
14
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. JavaScript: The Missing Manual Chapter 3: Adding Logic & Control, Part 2 Author: David S. McFarland | Publisher: O’Reilly Copyright 2010
  • 2. Handling Repetitive Tasks with Loops
    • Loop – performs the same task over and over again
    • Examples:
      • looping through a series of text boxes to identify the ones that are empty
      • looping through a series of checkboxes to identify the ones that are checked
    Copyright 2010
  • 3. While Loops
    • while loop – repeats a chunk of code as long as a particular condition is true
    • Syntax:
    • while (condition) {
    • // javascript to repeat
    • }
    Copyright 2010
  • 4. While Loops
    • Unlike a conditional statement, JavaScript interpreter sees a closing curly brace and “loops” back to repeat the code for as long as necessary
    • while (condition) {
    • // javascript to repeat
    • }
    Copyright 2010
  • 5. While Loops
    • Example #1 ( while_no.html )
    • document.write(“Number 1 <br />”);
    • document.write(“Number 2 <br />”);
    • document.write(“Number 3 <br />”);
    • document.write(“Number 4 <br />”);
    • document.write(“Number 5 <br />”);
    Copyright 2010
  • 6. While Loops
    • Example #2 ( while_yes.html )
    • var num = 1;
    • while(num <= 5) {
    • document.write(&quot;Number &quot; + num ► + &quot;<br />&quot;);
    • }
    Copyright 2010
  • 7. Loops and Arrays Copyright 2010
  • 8. For Loops
    • for loops vs. while loops
      • a little more compact
      • a little more confusing
    • Syntax:
    • for (parameter1; parameter2; parameter3) {
    • // javascript to repeat
    • }
    Copyright 2010
  • 9. For Loops
    • Example #3 ( for_yes.html )
    • var num = 1;
    • for(num = 1; num <= 5; num++) {
    • document.write(&quot;Number &quot; + num ► + &quot;<br />&quot;);
    • }
    Copyright 2010
  • 10. Do/While Loops
    • do/while loop – less commonly used
      • loop guaranteed to run at least once
    • Syntax:
    • do {
    • // javascript to repeat
    • } while (condition);
    Copyright 2010
  • 11. Functions: Turn Useful Code into Reusable Commands
    • function – a series of programming steps that you set up at the beginning of your script
      • code is not run immediately, but rather stored for later use
      • used for efficiently performing multiple programming steps repeatedly
    Copyright 2010
  • 12. Functions: Turn Useful Code into Reusable Commands
    • function – use same naming rules as with variables
    • Syntax:
    • function function_name() {
    • // javascript code you want to run
    • }
    Copyright 2010
  • 13. Functions: Mini-Tutorial
    • Example ( 3.2.html ):
    • Step #1: create function
    • function print_today() {
    • var today = new Date();
    • document.write(today.toDateString());
    • }
    Copyright 2010
  • 14. Functions: Mini-Tutorial
    • Example ( 3.2.html ):
    • Step #2: call the function
    • <p>Today is
    • <script type=“text/javascript”>
    • print_today();
    • </script>
    • </p>
    Copyright 2010
  • 15. Getting Information to Your Functions Copyright 2010
  • 16. Retrieving Information From Functions Copyright 2010
  • 17. JavaScript
    • blah, blah, blah . . .
    Copyright 2010

×