Menu
Hi,
I have this exercise where I have the following:
[CODE]<script type=”text/javascript”>
//grab user response from prompt
var userResponse=prompt(“Please enter some random words”, “”);
var userResponse2=prompt(“Please enter another set of words”, “”);
//split the user response into spaces
var userResponse_array=userResponse.split(” “);
document.write(userResponse_array.sort());
</script>
I’m trying to determine with the another input which way to sort the words alphabetically. Any ideas how this might be done?
[code=php]
<html>
<head>
<title>Sorted Words</title>
</head>
<script type="text/javascript">
function GetWords() {
//grab user response from prompt
var userResponse1=prompt("Please enter some random words", "");
var userResponse2=prompt("Please enter another set of words", "");
//split the user response into spaces
var userResponse_array1=userResponse1.split(" ");
var userResponse_array2=userResponse2.split(" ");
// display information entered in sorted order
document.getElementById('sortedList1').value = userResponse_array1.sort().join('n');
document.getElementById('sortedList2').value = userResponse_array2.sort().join('n');
}
</script>
<body>
<button onClick="GetWords()">Collect words</button><br />
<textarea id="sortedList1" rows="20"></textarea>
<textarea id="sortedList2" rows="20"></textarea>
</body>
</html>
[/code]
[CODE]var Wordman= {
tlcSort: function(a,b){
if(a[0]== b[0]) return 0;
if(a[0].toLowerCase() > b[0].toLowerCase()) return 1;
return -1;
},
grabWords: function(str,sorter){
var T=this;
var G= {}, A= [],word,W;
T.wordcount= 0;
var Rx= /([a-z]+((-|')[a-z]+)?)/ig;
while(str && (word= Rx.exec(str))!= null){
word= word[1].toLowerCase();
word= word.charAt(0).toUpperCase()+word.substring(1);
if(!G[word])G[word]= 0;
G[word]+= 1;
++T.wordcount;
}
for(var w in G){
A.push([w,G[w]]);
}
T.uniquewords= A.length;
if(sorter=== true){
A.sort(T.tlcSort);
return A;
}
A.sort(function(a,b){
if(b[1]== b[2])return T.tlcSort(a,b);
return b[1]-a[1];
})
return A;
}
}[/CODE]
[CODE]Wordman.demo=function(str){
var T=this;
var S= [];
S.push(T.grabWords(str).join('; '));
S.push(T.grabWords(str,true).join('; '));
if(T.uniquewords){
var s1= T.uniquewords+' unique words of '+
T.wordcount+ ' total wordsn';
alert(s1+'Alphabetic Sort:n'+S[0]);
alert(s1+'Use (Frequency) Sort:n'+S[1]);
}
else alert('No words found')
}
Wordman.demo(document.body.innerHTML);[/CODE]
[CODE]var Wordman= {
tlcSort: function(a,b){
if(a[0].toLowerCase()== b[0].toLowerCase()) return 0;
if(a[0].toLowerCase() > b[0].toLowerCase()) return 1;
return -1;
},
grabWords: function(str,sorter){
var T= this;
var G= {}, A= [],word,W;
T.wordcount= 0;
var Rx= /([a-z]+((-|')[a-z]+)?)/ig;
while(str && (word= Rx.exec(str))!= null){
word= word[1].toLowerCase();
word= word.charAt(0).toUpperCase()+word.substring(1);
if(!G[word])G[word]= 0;
G[word]+= 1;
T.wordcount+= 1;
}
for(var w in G){
A.push([w,G[w]]);
}
T.uniquewords= A.length;
if(sorter=== true){
A.sort(T.tlcSort);
return A;
}
A.sort(function(a,b){
if(b[1]== a[1]) return T.tlcSort(a,b);
return b[1]-a[1];
})
return A;
}
}[/CODE]
[CODE]Wordman.demo= function(str,boo){
var T= this;
var s= (boo=== true)?'Alphabetic Sort:n':'Use (Frequency) Sort:n';
var s2= T.grabWords(str,boo).join('; ');
if(T.uniquewords){
alert(T.uniquewords+' unique words of'+
T.wordcount+ ' total wordsn'+s+s2);
}
else alert('No words found')
}
Wordman.demo(document.body.innerHTML);
Wordman.demo(document.body.innerHTML,true);[/CODE]
Will this work for you? ?
[code=php]
<html>
<head>
<title>Sorted Words</title>
</head>
<script type="text/javascript">
function GetWords() {
//grab user response from prompt
var userResponse1=prompt("Please enter some random words", "");
var userResponse2=prompt("Please enter another set of words", "");
//split the user response into spaces
var userResponse_array1=userResponse1.split(" ");
var userResponse_array2=userResponse2.split(" ");
// display information entered in sorted order
document.getElementById('sortedList1').value = userResponse_array1.sort().join('n');
document.getElementById('sortedList2').value = userResponse_array2.sort().join('n');
}
</script>
<body>
<button onClick="GetWords()">Collect words</button><br />
<textarea id="sortedList1" rows="20"></textarea>
<textarea id="sortedList2" rows="20"></textarea>
</body>
</html>
[/code]
May need further modification if 'case' of the words entered is significant.[/QUOTE]
[CODE]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>4.7</title>
<head>
</head>
<body>
<script type="text/javascript">
function des(a,b){
if(a > b){
return 1;
}
else{
return -1;
}
}
function GetWords() {
//grab user response from prompt
var userResponse1=prompt("Please enter some random words", "");
var userResponse2=prompt("Would you like to sort the array in Ascending order or Descending order?", "");
//split the user response into spaces
var userResponse_array1=userResponse1.split(" ");
var userResponse_array2=userResponse2.split(" ");
// display information entered in sorted order
var a=userResponse2.substring(0,1);
//alert(userResponse_array1.length);
if(a=="A"){
document.getElementById("here").innerHTML=userResponse_array1.sort();
}
else{
document.getElementById("here").innerHTML=userResponse_array1.sort(des);
}
}
</script>
<body>
<p id="here"></p>
<button onClick="GetWords()">Collect words</button><br />
</body>
</html> [/CODE]
[code=php]
<html>
<head>
<title>Sorted Words</title>
</head>
<script type="text/javascript">
var userResponse_array = new Array();
function GetWords() {
//grab user response from prompt
var userResponse=prompt("Please enter some random words", "");
//split the user response into spaces
userResponse_array=userResponse.split(" ");
// display information entered in entered order
document.getElementById('entryList').value = userResponse_array.join('n');
}
function SortUpWords() {
userResponse_array.sort();
document.getElementById('sortedList').value = userResponse_array.join('n');
}
function SortDownWords() {
userResponse_array.reverse();
document.getElementById('sortedList').value = userResponse_array.join('n');
}
</script>
<body>
<button onClick="GetWords()">Collect words</button>
<button onClick="document.getElementById('entryList').value=''">Clear</button>
<br />
<textarea id="entryList" rows="20"></textarea>
<textarea id="sortedList" rows="20"></textarea><br />
<button onClick="SortUpWords()">Ascending Sort</button>
<button onClick="SortDownWords()">Decending Sort</button>
</body>
</html>
[/code]
0.1.9 — BETA 5.22