Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Looping and Arrays 1

Status
Not open for further replies.

tnbrwneyez

Programmer
Oct 2, 2004
19
US
I need to loop the names entered and have them to list alphabetically and I cannot figure out how. Can anyone helo me? Here is my script:

<html>
<head>
<title>Loops Example</title>
</head>

<body>
<h1>Loop Example</h1>

<Script LANGUAGE="JavaScript1.2" type="text/javascript1.2">
names = new Array();
i=0;
do {
next=window.prompt("Enter the Next Name", "");
if (next > "") names =next;
i=i+1;
}
while (next > "");
document.write("<h2>" + (names.length) + " names entered. </h2>");
document.write("<ol>");
for (i in names){
document.write("<li>" + names + "<br>");
}

document.write("</ol>");
</script>
</body>
</html>

I would appreciate any help that I can get.

Thanks
 
You can use the sort() method to alphabetize your array.
The code will look like this.
Code:
<html>
<head>
<title>Loops Example</title>
</head>

<body>
<h1>Loop Example</h1>

<Script LANGUAGE="JavaScript1.2" type="text/javascript1.2">
names = new Array();
i=0;
do {
    next=window.prompt("Enter the Next Name", "");
if (next > "") names [i] =next;
i=i+1;
}
while (next > "");
document.write("<h2>" + (names.length) + " names entered. </h2>");
document.write("<ol>");
names.sort();
for (i in names){
document.write("<li>" + names [i] + "<br>");
}

document.write("</ol>");
</script>
</body>
</html>
Hope that helps
Matt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top