Alphabetizer Program - PLEASE help me

Ask questions about projects relating to: computer science or pure mathematics (such as probability, statistics, geometry, etc...).

Moderators: AmyCowen, kgudger, bfinio, MadelineB, Moderators

Locked
Addie11h
Posts: 2
Joined: Mon Jan 19, 2015 1:26 pm
Occupation: Student 8th Grade
Project Question: I am working on the ABCs of Programming: Writing a Simple "alphabetizer" with JavaScript. Can someone please give me some hints about what commands need to go in what sequence? I don't even know where to start on this. Thanks!
Project Due Date: 1/25/15
Project Status: I am conducting my research

Alphabetizer Program - PLEASE help me

Post 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>
HowardE
Posts: 496
Joined: Thu Nov 20, 2014 1:35 pm
Occupation: Science Buddies content developer
Project Question: N/A
Project Due Date: N/A
Project Status: Not applicable

Re: Alphabetizer Program - PLEASE help me

Post by HowardE »

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.
Addie11h
Posts: 2
Joined: Mon Jan 19, 2015 1:26 pm
Occupation: Student 8th Grade
Project Question: I am working on the ABCs of Programming: Writing a Simple "alphabetizer" with JavaScript. Can someone please give me some hints about what commands need to go in what sequence? I don't even know where to start on this. Thanks!
Project Due Date: 1/25/15
Project Status: I am conducting my research

Re: Alphabetizer Program - PLEASE help me

Post by Addie11h »

IT WORKS!!! Thank you very, very much. I could not have done this without your help.
HowardE
Posts: 496
Joined: Thu Nov 20, 2014 1:35 pm
Occupation: Science Buddies content developer
Project Question: N/A
Project Due Date: N/A
Project Status: Not applicable

Re: Alphabetizer Program - PLEASE help me

Post by HowardE »

You're welcome. You'll get better with debugging and using the developer's tools as you do more projects like this.

Howard
Locked

Return to “Grades 6-8: Math and Computer Science”