Click Refresh (or Reload) to run the script again.
Here is the code that generates different types of lists based on the user's choice. It uses the DOM rather than the outmoded write/ln()approach. Note that we put the <script> block at the bottom of the XHTML markup so that the page has completely rendered before the code in the block executes. This is required because the code looks for a markup element within the page which must already exist (i.e. be rendered) before it can be used.
var strChoice, // user's choice
strStartTag, // starting list item tag
strEndTag, // ending list item tag
boolValidInput = true, // flag that indicates if input is valid
strListType; // list type as a string
strChoice = window.prompt( "Select a list style:\n1 (bullet), 2 (numbered), 3 (lettered)", "1" );
switch ( strChoice ) {
case "1":
strStartTag = "<ul>";
strEndTag = "</ul>";
strListType = "<h1>Bullet List</h1>"
break;
case "2":
strStartTag = "<ol>";
strEndTag = "</ol>";
strListType = "<h1>Ordered List: Numbered</h1>"
break;
case "3":
strStartTag = "<ol type = 'A'>";
strEndTag = "</ol>";
strListType = "<h1>Ordered List: Lettered</h1>"
break;
default:
// Other choices are invalid so set the flag for that
boolValidInput = false;
}
if ( boolValidInput == true )
{
var strDisplay = "<h2>Code Output:</h2";
strDisplay += strListType +
strStartTag;
for ( var i = 1; i <= 3; ++i )
strDisplay += "<li>List item " + i + "</li>";
strDisplay += strEndTag;
}
else
strDisplay += "Invalid choice: " + strChoice;
document.getElementById("output").innerHTML = strDisplay;