Page 1 of 1

Alphabetizer Program - PLEASE help me

Posted: Thu Jan 22, 2015 10:26 am
by Addie11h
I am working on the Science Buddy project to write a simple alphabetizer program. I am getting an error I can't resolve. It has to do with outputting the sorted text. Can somebody please help me? My project is almost due. Here is my code, but I am also uploading it as an attachment. THANKS SO MUCH.

<HTML>
<HEAD>
<TITLE>Alphabetizer</TITLE>
<HEAD>
<script language="JavaScript">
function SortText(inText, outText)
{
//First we declare a variable to store the input in
//Now we split the block of text into separate words
var wordArray = inText.split(" ");

//Next we call the sort method to put input array into order
wordArray.sort();

//Finally we use the .join method to get the alphabetized list back into the form of a single string to put in output TEXTAREA
outText.value = wordArray.join(" ");
}

</script>

<!--to access the last element in our array-->
<!--inputTextArray[inputTextArray.length - 1]-->

</HEAD>
<BODY>
<FORM id="TheForm">
<!--makes box for user to input list of words-->
<p><span>Type or paste a list of words, seperated by spaces, into the box below, then press the "Alphabetize" button.</span></p>
<TEXTAREA id="input" name="inputText" rows=5 cols=80 wrap=on></TEXTAREA>
<br /><br />
<input type="button" name="btnAlphabetize" value="Alphabetize" onclick="SortText(this.form.input, this.form.output);">

<!--makes box for alphabetized list-->
<p><SPAN>Your alphebatized list goes here!!!</span></p>
<TEXTAREA id="output" name="outputText" rows=5 cols=80 wrap=on readonly></TEXTAREA>


</FORM>
</BODY>
</HTML>

Re: Alphabetizer Program - PLEASE help me

Posted: Thu Jan 22, 2015 11:44 am
by deleted-249560
It would help to explain what error you're getting if you want help debugging. Most browsers have developer tools that will bring up a console of some kind - that's a great way to have the browser tell you the problem in detail.

In this case, I didn't see an issue with your outputting text, but you were trying to split a string into an array without the Javascript interpreter knowing your data type properly. I had your function copy the input into a variable first and then it seemed better:

Code: Select all

function SortText(inText, outText)
{
//First we declare a variable to store the input in
//Now we split the block of text into separate words
var tempStr = inText.value;  // This is the added line, then the next one splits the temp string
var wordArray = tempStr.split(" ");

//Next we call the sort method to put input array into order
wordArray.sort();

//Finally we use the .join method to get the alphabetized list back into the form of a single string to put in output TEXTAREA
outText.value = wordArray.join(" ");
}
See if that fixes your problem.

Re: Alphabetizer Program - PLEASE help me

Posted: Thu Jan 22, 2015 8:55 pm
by Addie11h
IT WORKS!!! Thank you very, very much. I could not have done this without your help.

Re: Alphabetizer Program - PLEASE help me

Posted: Fri Jan 23, 2015 10:37 am
by deleted-249560
You're welcome. You'll get better with debugging and using the developer's tools as you do more projects like this.

Howard