JavaScript
  Home arrow JavaScript arrow Page 3 - Introducing JavaScript Functions and Loops
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

Introducing JavaScript Functions and Loops
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 7
    2007-11-12

    Table of Contents:
  • Introducing JavaScript Functions and Loops
  • Putting the Fun in Your Functions
  • Loops
  • While Loop

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Introducing JavaScript Functions and Loops - Loops


    (Page 3 of 4 )

    Another way for programmer's to be lazy is with loops. Loops are used when you want to repeat a task several times. There are two types of loops in JavaScipt: the For Loop and the While Loop.

    For Loop

    For Loops are used when you wish to define how many times you want to loop a certain code. Here is a program that counts down the number of beer bottles on a wall.


    <html>

    <body>


    <script type="text/javascript">

    for (i = 5; i >= 1; i--)

    {

    document.write(i + " Bottles of beer on the wall")

    document.write("<br />")

    }

    </script>

    </body>

    </html>

    This code would result in the following print out:

      5 Bottles of beer on the wall
      4 Bottles of beer on the wall
      3 Bottles of beer on the wall
      2 Bottles of beer on the wall
      1 Bottles of beer on the wall

    The For loop takes place in the line: for (i = 5; i >= 1; i--). Let's take a look at what each section does.

    The i = 5 assigns the value of 5 to our counter variable. Next, we use the i >= 1 to tell the loop to continue looping while the value of i is greater than or equal to 1. Finally, we use i-- to decrease the value of i by 1 each time through the loop. You could of course also write a loop where you used the increment operator instead of the decremental operator.

    Note that it isn't necessary to name the counter variable i. You can name it whatever you like; it is simply programming practice to write i.

    Before we go on to the While loop, let's look at one more cool kind of code:


    <html>

    <body>


    <script type="text/javascript">

    for (i = 1; i <= 6; i++)

    {

    document.write("<h" + i + ">I'm shrinking!")

    document.write("</h" + i + ">")

    }

    </script>


    </body>

    </html>

    With this code, we use the header tag to make our text appear to shrink each time through the loop. The result is this:

     

      I'm shrinking!

      I'm shrinking!

      I'm shrinking!

      I'm shrinking!

      I'm shrinking!
      I'm shrinking!

    If we wanted to reverse it, we could use this code:


    <html>

    <body>


    <script type="text/javascript">

    for (i = 6; i >= 1; i--)

    {

    document.write("<h" + i + ">I am becoming the world's fattest man!")

    document.write("</h" + i + ">")

    }

    </script>


    </body>

    </html>

    The result would be:


      I am becoming the world's fattest man!
      I am becoming the world's fattest man!

      I am becoming the world's fattest man!

      I am becoming the world's fattest man!

      I am becoming the world's fattest man!

      I am becoming the world's fattest man!

    More JavaScript Articles
    More By James Payne


       · Thanks for dropping by for my discussion on Javascript functions and loops. Here we...
     

    JAVASCRIPT ARTICLES

    - Validating Digits and Dates with jQuery`s Va...
    - Validating Ranges, Emails, and URLs with jQu...
    - More Uses for the jQuery Tooltip Plug-in`s b...
    - Building Image-Based Tooltips with the jQuer...
    - Using the jQuery Tooltip Plug-in`s bodyHandl...
    - Using Rangelength, Min and Max with the Vali...
    - Using Minlength and Maxlength with the Valid...
    - Modifying Tooltip Coordinates with the jQuer...
    - Applying a Fade Out Effect with the jQuery T...
    - Tracking Mouse Movements with the jQuery Too...
    - Checking Online Forms with the Validator jQu...
    - Nested JavaScript Functions as Objects
    - The jQuery Tooltip Plug-in
    - Active Client Pages at the Server
    - ACP Tab Web Page







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    Stay green...Green IT