Skip to content

Instantly share code, notes, and snippets.

@apipkin
Forked from anonymous/gist:347873
Created March 29, 2010 14:30
Show Gist options
  • Save apipkin/347885 to your computer and use it in GitHub Desktop.
Save apipkin/347885 to your computer and use it in GitHub Desktop.
var names, array, size, order, str, sort, reverse, rev;
names = prompt("Please input names separated by spaces: ", "");
order = prompt("What order? (ascending or descending)", "");
array = names.split(/\s+/);
size = array.length;
if (names == "" || names == null)
{ document.write("Oops, you did not enter a name, please enter a name!"); }
else if (names != "" && names != null)
{ document.write("You have entered the following names: " + "<br />"); }
for (var i = 0; i < array.length; i++)
{ document.write(array[i] + "<br />"); }
document.write("<br />" + "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-" + "<br />" + "<br />");
if (order == "")
{ document.write("Error - order is incorrectly specified!"); }
else if (order == null)
{ document.write("Error - order is incorrectly specified!"); }
str = order.toLowerCase();
if (str == "ascending") { sort() }
else if (str == "descending") { rev() }
function sort() {
if (str == "ascending" )
{ sort = array.sort(); document.write("Here are the names in Ascending order: " + "<br />"); }
else if (str != "ascending")
{ document.write("Error - order is incorrectly specified!"); }
for (var i = 0; i < array.length; i++)
{ document.write(sort[i] + "<br />"); }
}
function rev() {
if (str == "descending")
{ rev = array.reverse(); document.write("Here are the names in Descending order: " + "<br />"); }
else if (str != "descending")
{ document.write("Error - order is incorrectly specified!"); }
for (var i = 0; i < array.length; i++)
{ document.write(rev[i] + "<br />"); }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment