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. If arrays.html is not already open, open it in NotePad++. Save the file as arrays_for_loop.html. 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." 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>'); Save arrays_for_loop.html again. 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
In this script, you will simplify the body script using a for loop. During each loop, one book title will print to the screen.
document.write('<ol>'); for (var i=0; i<books.length; i++) { document.write('<li>'+books[i]+'</li>'); } document.write('</ol>');
for (var i=0; i < arrayName.length; i++) { //put repeating code here }