JavaScript Missing Manual, Chapter 3, part 2

1,042 views

Published on

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
1,042
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
17
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

JavaScript Missing Manual, Chapter 3, part 2

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

×