Bigman1981
11-03-2003, 10:57 AM
Can somebody help, im rather new to Javascript and i would like to know about arrays.
I want to know how you can prompt for values using "window.prompt" and fill an array. After entering the values i would like to print the array to the screen using "document.write"
Any help will be greatly appreciated, and if possible some example coding would be of help
Thanks:)
fredmv
11-03-2003, 11:55 AM
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>untitled</title>
<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=iso-8859-1" />
<script type="text/javascript">
//<![CDATA[
/*
First, create a new array using the Array constructor and store
it in a variable called theArray.
*/
var theArray = new Array();
/*
Now we need to prompt the user for a number, we can do this using the prompt method.
This number will be the length of the array.
*/
var sizeOfArray = parseInt(prompt("How many items will the array have?", ""));
/*
This next line may look complex, but once you understand what is going on, it is actually
quite simple. This is called a for loop and is a control structure in JavaScript (and many other
programming languages, for that matter). Alright, so lets look at the first part:
for(i=0;
This is generally referred to as the initalization stage of the loop. This tells the loop
to start off at zero. Now that the variable i is defined as 0, we can now work with it.
Now for the next part of the for loop:
i<sizeOfArray;
This line almost explains itself - keep doing everything after the curleybrace until i is
less than the sizeOfArray variable. Remember that we previously prompted the user to enter
a value for this variable, the loop will keep comparing the value that was entered and stored in the
sizeOfArray variable to the current state of the i variable.
Finally, the last part of the for loop:
i++)
What this does is increment the current value of the i variable. The term increment
is just another way of saying adding 1 to.
And that's all there really is to a for loop.
*/
for(i=0; i<sizeOfArray; i++)
{
/*
You see how in the loop, we keep incrementing the value of the i variable? What we're doing
here is setting the current cell of the array to whatever i is currently equal to. For the value of
this specific cell, we're using the prompt method again to ask for some user input.
*/
theArray[i] = prompt( "What will the value be for cell number " + i + "?", "" );
}
/*
Here we go again with the for loop. The exact same idea is used here.
*/
for(i=0; i<theArray.length; i++)
{
/*
Now we're using the document object's writeln method. It works just like the write
method except for one small difference - it appends a newline (\n) character to the end of the output.
All we're doing here is going through every cell in the array and printing its value and then using a break (<br />)
to display each array cell value on its own line.
*/
document.writeln("theArray[" + i + "] = " + theArray[i] + "<br />");
}
/*
And we're done! I hope that clears things up for you.
*/
//]]>
</script>
</head>
<body></body>
</html>