Using a script to save yourself some time populating a multi-dimensional array
Take the following code sample and see if you can improve on it, as per the suggestions in the code's comments.
<html>
<head><title>Using a script to save yourself some time populating a multi-dimensional array</title></head>
<body>
<h2>Using a script to save yourself some time populating a multi-dimensional array</h2>
<script language="JavaScript" type="text/javascript">
<!--
/* this script will use prompts to gather information that's destined for an array, and will output some final array code ready to paste into your Javascript--essentially, using a JavaScript to create a more complex JavaScript */
var mainVariable=prompt("Enter the main variable name");//change these following prompts to match whatever you're collecting
var personName=prompt("Enter the first person's name");
var personAddress=prompt("What's their address?");
var personWardrobe=prompt("What do they wear?");/* Challenges:
Replace the hard-coded variables above with some prompts that let the user put the information into the script, instead of requiring you to change the script:
1) First, prompt the user: How many subsets will this main variable have?
2) Now, use that information to start a loop:
Subset 1: Enter a variable name for this subset.
Subset 1: Enter a short description for this subset.
Continue this loop till you've got all the variable names.
3) Now, rewrite the three var statements above so they take the info from the prompts and put them into new prompts to gather the rest of the information.
*///now we use the script to cough up the desired output
document.write("var "+mainVariable+" = newArray()<br/><br />");
document.write(mainVariable+"[0] = newArray();<br />");
document.write(mainVariable+"[0][0] = \""+ personName + "\";<br />");
document.write(mainVariable+"[0][1] = \""+ personAddress + "\";<br />");
document.write(mainVariable+"[0][2] = \""+ personWardrobe + "\";<br />");/* Replace these document.write statements with a loop. Replace the hard-coded variable names personName, personAddress, personWardrobe with the variable names collected via your prompts above. */
//-->
</script><p>Copy the above code and paste it into the appropriate place in your JavaScript.</p>
</body>
</html>