Lab 5—Your First JavaScripts

Part 2—Print array to screen using a for loop


In this script, you will simplify the body script using a for loop. During each loop, one book title will print to the screen.

 

  1. If arrays.html is not already open, open it in NotePad++.
  2. Save the file as arrays_for_loop.html.
  3. Change the title of your page to "Array Script with For Loop by " and your name; for example, "Array Script with For Loop by Joy."
  4. Change the body script, to read as follows:

    document.write('<ol>');
    for (var i=0; i<books.length; i++)
    {
        document.write('<li>'+books[i]+'</li>');
    }
    document.write('</ol>');


  5. Save arrays_for_loop.html again.
  6. In Firefox, open arrays_for_loop.html. Do all seven books print to the screen? If not, use the Firefox Error Console to find and fix and errors.

 

 
previous page   next page

 

 

Syntax for For Loops

for (var i=0; i < arrayName.length; i++)
{
    //put repeating code here
}