background
|
|
RiscOS
Risc OS Info
All you need to know about Risc OS and more.
More icon

News
All in one Place
All of your regular news in one place.
More icon
|
|
Up Image
Navigation
Search this Site
Enter your search terms

Site Breadcrumb - You are here
|
Thau's JavaScript Tutorial
Lesson 4

by Thau!

Page 6 — Nested Loops

Here's the script:

var height = prompt("How high do you want the grid? 
(1-10 is good)","10");

var width= prompt("How wide do you want the grid? 
(1-10 is good)","10");

var a_line;

var new_window = 
window.open("grid.html","looper",
"width=400,height=400");
new_window.document.writeln("<h1>A Grid</h1>");
for (var height_loop = 0; height_loop < height; height_loop++) 
{	
	a_line = "";
	for (var width_loop = 0; width_loop < width; width_loop++)
	{
		a_line+="x";
	}
	new_window.document.writeln(a_line + "<br>");
}

After asking for height and width, opening a new window, and writing a header to it, we go into a for loop. The first for loop sets a_line = "". Try doing the example without this line and see what happens. After initializing a_line, the script goes into another for loop to build a line of x's as wide as required and prints it out to the new window. This happens height times.

OK, here's your assignment: Take a look at this loop exercise and try doing it on your own before viewing source to see how it works.

next page»