Created
March 29, 2010 14:18
-
-
Save anonymous/347873 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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