View Full Version : Sorting words and searching a string

10-23-2005, 10:10 PM
Hello there! I am working on a little project where I have a form that allows a user to input a sentence that will be sorted alphabetically when they click sort. The result should be displayed in the Output box at the bottom. Then They should be able to enter a string that will be search from the previous sentence entered and that search will display in the output as well. Then last they should be able to enter in a replacement string; however, I have not done much reading on that so I am completely clueless there. I thought I would start with the small stuff and last I would work on the replacement string. So I have the form done, but I seem to be struggling with the functions. I think I should use aname.sort to sort the words in ABC order. I am wondering if I am just completely off on how I am trying to do all of this. Any tips would be great, just to maybe point me in the right direction. Thank you so much!!

<?xml version = "1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

<html xmlns = "http://www.w3.org/1999/xhtml">
<title>Lab 2 Part B</title>

<script type = "text/javascript">

function outputArray( head, theArray)
= "\n" + head + theArray.join(" ") + "\n";

function sortButton()
var wordsSplit = document.forms.myForm.words.value.split(/ +/);
var sorted = wordsSplit.sort(
return (a.toLowerCase() > b.toLowerCase())? 1:-1

"The words in your sentence are now in alphabetical order: ",

function searchbuttonPressed()
= "\n" + document.writeln( "<br /> sentence.charCodeAt( searchstring ) + "</p>" );

function replacebuttonPressed()


// -->

<form name = "myForm" action = "">
<p>Enter a sentence to work with:<br />
<input name = "words" type = "text" size = "50">
<input name = "sorted" type = "button" value =
"Sort" onclick = "sortButton()" /></p>

<p>Enter a string to search for:<br />
<input name = "searchstring" type = "text" size = "50"/>
<input name = "search" type = "button" value = "Search"
onclick = "searchbutton()" /><br /></p>

<p>Enter a replacement string to search for:<br />
<input name = "replacestring" type = "text" size = "50"/>
<input name = "replace" type = "button" value = "Replace"
onclick = "replacebutton()" /><br /></p>

<p>Output:<br />
<textarea name = "output" rows = "8" cols = "34">


10-24-2005, 01:55 PM
You did excelent work, just keep going.