<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<!-- Program by Maria Kelly
-->
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Sorting a line of text</title>
</head>
<body>
<script type = "text/javascript" >
<!--
var index;
//Prompt user for input
var str1 = prompt("Please enter a line of lowercase text.", "");
//Prompt user for input
var str2 = prompt("Would you like the text to be sorted in ascending or descending order?", "");
//Split the text entered by user an insert into an array
var inputArray = str1.split(" ");
While (str2 = "descending"){
//Sort the array in descending order
inputArray.reverse();
//Display the array in reverse order
for (index = 0; index < inputArray.length; index++)
document.write(inputArray[index], "<br />");
}
While (str2 = "ascending"){
//Sort the array in ascending order
inputArray.sort();
//Display the array in ascending order
for (index = 0; index < inputArray.length; index++)
document.write(inputArray[index], "<br />");
}
// -->
</script>
</body>
</html>
|