sorting a textarea.

T

Totti

Hi all,
I am a newbie to javascript learning it for 2 months now, i am trying
to make a sorter who will read english words from the one tex area or
data file and show the result in the 2nd text area, in other words,
howerever the words are set, horizontally the one following the other
or vertically, the one under the other. i want it to work, i tried
some code but i am having problems it only work when the text entered
is horizontal like the one word following the other when i try to
enter a second word after pressing ENTER, i dont get any result,

Example : say i have " to be or not to be,this is the question" i want
to have "BE BE,THIS IS NOT OR QUESTION THE TO TO" this seems correct
but actually it is not because the "," ascii is less than the letters
so it should go first, and the second problem is that there are spaces
in the output that i can not understand where they are coming from and
i dont know how to deal with them.

the second problem is that if i enter:
to
be
or
not
to
be,
this
is
the
question

pressing enter after each word the script doesnt seem to work at all
it just copy the input to the output, instead i want to make it do the
following :
",
BE
BE
THIS
IS
NOT
OR
QUESTION
THE
TO
TO
"

here is my code:

<html>
<head>
<title>Numbers and Words Sorter</title>
</head>
<body>
<b>Sorting Machine</b><p>

<script type="text/javascript">
function sortalpha(x,y) {
return(x-y)
}
function sortvalues(param) {
var inputvalues =
document.Form.textInput.value.toUpperCase().split(" ")
if (param==0) {
inputvalues.sort() }
else
inputvalues.sort(sortalpha)
for (i=0; i < inputvalues.length-1; i++)
document.Form.textOutput.value =
document.Form.textOutput.value
+inputvalues + " "
{document.Form.textOutput.value +=
inputvalues[inputvalues.length-1] }
}
</script>

<form name="Form">
<textarea rows="10" name="textInput" cols="30" wrap="virtual">
</textarea>
<textarea rows="10" name="textOutput" cols="30" wrap="virtual">
</textarea>
<p>
<input type = "button" value="Sort Letters" name="Button"
onClick="sortvalues(0)">
<br>
<input type = "button" value="Sort Numbers" name="Button"
onClick="sortvalues(1)">
<br>
<input type = file name ="file" size =15>
<br>
<input type = "button" value="print" name="Button"
onClick="print(document.Form.textOutput.value)">
<br>
<input type = "reset" value="Reset" name="Button">
</form>
</body>
</html>
===============================================================

this code works in some cases but in others it doesnt for example in
the case of parenthesis, the parenthesis do not go before the letters
eventhough their ASCII is < , and it generates empty spaces in the
output which i can not explain, i know it is something wrong in my
code but i need your help please pointing out the problem.

I want to make this program work perfect, and i want to add more
things to it, but for the time being this is the obvious problem i am
infront,
Thanks in advance for any help!
 
T

Thomas 'PointedEars' Lahn

Totti said:
I am a newbie to javascript learning it for 2 months now,

You don't have to mention that, it is all-too-obvious. There is no
"javascript", BTW.
i am trying to make a sorter who will read english words from the one tex
area or data file and show the result in the 2nd text area, in other
words, [one incredibly long, nested, unfinished all-lowercase sentence
snipped]

You are not nearly clever enough to do that or understand solutions
presented to that end here yet. The sooner you get that, the sooner
you can stop whining about your 2-month-newbie status in each of your
postings and start developing software for a change.

<http://catb.org/~esr/faqs/smart-questions.html>


Score adjusted

PointedEars
 
T

Totti

OHHH einstein is back,
he is judging people smartness!!!

Hey cool guy act like a pro. not like a kiddy,
if you wana help then do so if not just forget about the thread and
click the X on the upper left of your screen to close and go to
another one. without a lot of blablabla... for god's sake
 
T

Thomas 'PointedEars' Lahn

Totti said:
OHHH einstein is back,

While I do appreciate the sentiment, I can understand that for someone with
an intellect like a mothball, it must seem like any other person is a genius.
he is judging people smartness!!!

I am judging your smartness, or rather the demonstrated lack thereof.
Hey cool guy act like a pro. not like a kiddy,

Says the script-kiddy.
if you wana help then do so if not just forget about the thread and
click the X on the upper left of your screen to close

There is no X on the upper left on my screen. So much for your competence.
and go to another one.

Or you could simply FOAD. Usenet is not a right, and if you cannot
behave you will get no help. No competent one at least.
without a lot of blablabla... for god's sake

Says the person who turns out to be a master of blablabla.


PointedEars, f1
 
S

Sn!pe

Totti said:
I d rather leave you to your personal and psychological complexes.
If i say that i am just learning Javascript it is because i am a
mathemtician not a computer scientist, second i said 2 months because
i am a bigenner so the people who might want to help(except you of
course) know that i have no previous knowledge in Javscript and i am
learning it on my own.
BTW if you feel you are a genius so be it kiddy!!

Oh look, the comp.lang.javascripters have come to visit us again here
in good Olde Wundee! Hi guys, did you bring any beer this time?
 
L

Lasse Reichstein Nielsen

Totti said:
I am a newbie to javascript learning it for 2 months now, i am trying
to make a sorter who will read english words from the one tex area or
data file and show the result in the 2nd text area, in other words,
howerever the words are set, horizontally the one following the other
or vertically, the one under the other. ...
Example : say i have " to be or not to be,this is the question" i want
to have "BE BE,THIS IS NOT OR QUESTION THE TO TO" this seems correct
but actually it is not because the "," ascii is less than the letters
so it should go first,

That's not what you said above. There you said that you wanted to sort
words. A comma is not a word. I.e., you need to be more precise about
the problem you are trying to solve.

I'll assume for now that you want to split the text into sequences of
letters or puntuation and sort these alphabetically.
and the second problem is that there are spaces
in the output that i can not understand where they are coming from and
i dont know how to deal with them.

Ok, so you don't want spaces in the input to be represented in the
output at all.
the second problem is that if i enter:
to
be ....

pressing enter after each word the script doesnt seem to work at all
it just copy the input to the output, instead i want to make it do the
following :

....
Yes, this is what your code is doing.
here is my code:
....
<script type="text/javascript">
function sortalpha(x,y) {
return(x-y)
}

The name suggests that this compares alphabetically, while it actually
tries to convert the elements to numbers and compare them numerically.
The problem is that non-numbers will all be converted to NaN, which
is not a valid return value from a comparison function, so you need to
be sure that it's only used on numbers.
function sortvalues(param) {
var inputvalues =
document.Form.textInput.value.toUpperCase().split(" ")

Here you split the text on a single space. It doesn't split on
newlines (your problem from above), and it doesn't split between
punctuation and letters, and it doesn't handle multiple consequtive
spaces

Try:
var text = document.forms['Form'].elements['textInput'].value;
var inputvalues = text.toUpperCase().split(/\s+|\s*\b\s*/);

This will split between letters and punctuation or at any sequence
of whitespaces.
if (param==0) {
inputvalues.sort() }
else
inputvalues.sort(sortalpha)

What *is* sortalpha supposed to do? The default behavior of the sort
method is to sort the array's elements alphabetically as strings.
for (i=0; i < inputvalues.length-1; i++)
document.Form.textOutput.value =
document.Form.textOutput.value
+inputvalues + " "
{document.Form.textOutput.value +=
inputvalues[inputvalues.length-1] }
}


Here you could use the join method:

document.forms['Form'].elements['textOutput'].value =
inputvalues.join(" ");


In short: Before solving a problem, make sure that you have the
problem specified exactly. Then check that the functions you use
actually do what you want.

/L
 
L

Lasse Reichstein Nielsen

Thomas 'PointedEars' Lahn said:
There is no "javascript", BTW.

Sure there is. The word is in common use. You might not agree with its
use, but it's there.
You are not nearly clever enough to do that or understand solutions
presented to that end here yet. The sooner you get that, the sooner
you can stop whining about your 2-month-newbie status in each of your
postings and start developing software for a change.

<http://catb.org/~esr/faqs/smart-questions.html>

While ESR has some good points about dealing with the anti-social
behavior of geeks, it should not be seen as an endorsement or
recommendation of such behavior.

/L
 
T

Thomas 'PointedEars' Lahn

Lasse said:
Sure there is. The word is in common use.
You might not agree with its use, but it's there.

That incompetent people, and sometimes even people competent in other areas,
call anything anything is no proof that a designation is a correct one.
While ESR has some good points about dealing with the anti-social
behavior of geeks, it should not be seen as an endorsement or
recommendation of such behavior.

You miss the point.


PointedEars
 
T

Totti

Lasse, Thanks a lot.
you pointed out many misunderstood concepts, and made me think twice
about what i am doing. the "(/\s+|\s*\b\s*/);" actually kind of solved
the problem.
there is something else if you please help with, if the input is a
series of words entered vertically, with your "split(/\s+|\s*\b\s*/);"
the output is arranged, but horizontally. how can i make my script
arrange horizontal input horizontally and vertical input vertically?
it should be somewhere in the way the script writes the output,

should i put like an if statement that will check for the "enter" in
the input and if it finds it write out vertically , else horizontally?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,222
Members
46,810
Latest member
Kassie0918

Latest Threads

Top