2 dimensional array - sorting mechanism

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Totti

    2 dimensional array - sorting mechanism

    Hi everyone,
    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 textbox or
    datafile and store them in a 2 dimensional array, so in other words,
    howerever the words are set, horizontally the one following th eother
    or 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,

    here is my code:

    <html>
    <head>
    <title>Number s and Words Sorter</title>
    </head>
    <body>
    <td><b>Sortin g Machine</b></td><p>

    <script type="text/javascript">

    function sortalpha(x,y)
    {
    return(x-y)
    }

    function sortvalues(para m)
    {
    var inputvalues =
    document.Form.t extInput.value. toUpperCase().s plit(" ")

    if (param==0)

    inputvalues.sor t()

    else

    inputvalues.sor t(sortalpha)

    for (i=0; i < inputvalues.len gth-1; i++)

    document.Form.t extOutput.value = document.Form.t extOutput.value
    +inputvalues[i] + " "

    document.Form.t extOutput.value += inputvalues[inputvalues.len gth-1]

    }
    </script>

    <form name="Form">

    <textarea rows="10" name="textInput " cols="30" wrap="virtual">
    </textarea>
    <textarea rows="10" name="textOutpu t" cols="30" wrap="virtual">
    </textarea>
    <p>
    <input type = "button" value="Sort Letters" name="Button"
    onClick="sortva lues(0)">
    <br>
    <input type = "button" value="Sort Numbers" name="Button"
    onClick="sortva lues(1)">
    <br>
    <input type = file name ="file" size =15>
    <br>
    <input type = "button" value="print" name="Button"
    onClick="print( document.Form.t extOutput.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!
  • Jeremy J Starcher

    #2
    Re: 2 dimensional array - sorting mechanism

    On Thu, 30 Oct 2008 05:53:12 -0700, Totti wrote:
    Hi everyone,
    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 textbox or
    datafile and store them in a 2 dimensional array, so in other words,
    howerever the words are set, horizontally the one following th eother or
    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,
    I'm not exactly sure what your code is trying to do. If you could post
    your input and the EXPECTED output, that might help.

    Meanwhile, here are a few pointers on things that looked strange to me.

    <html>
    <head>
    <title>Number s and Words Sorter</title</head>
    <body>
    <td><b>Sortin g Machine</b></td><p>
    (Bad markup, you should remove the "<td></td>")
    <script type="text/javascript">
    >
    function sortalpha(x,y)
    {
    return(x-y)
    This is a numeric test. With strings it ALWAYS returns the value NaN.
    (You should be using compare operators here.)
    }
    >
    function sortvalues(para m)
    {
    var inputvalues =
    document.Form.t extInput.value. toUpperCase().s plit(" ")
    This particular split leaves EOL (end of line) markers in place.
    >
    if (param==0)
    >
    inputvalues.sor t()
    >
    else
    >
    inputvalues.sor t(sortalpha)
    I strongly suggest you put braces here. Not putting them in tends to
    bite you later on.

    Like this:
    if (param==0) {
    inputvalues.sor t();
    } else {
    inputvalues.sor t(sortalpha);
    }



    >
    for (i=0; i < inputvalues.len gth-1; i++)
    >
    document.Form.t extOutput.value =
    document.Form.t extOutput.value
    +inputvalues[i] + " "
    Likewise, I could include braces around this one-statement block as a
    matter of style.
    >
    document.Form.t extOutput.value += inputvalues
    [inputvalues.len gth-1]
    >
    }
    </script>
    >
    <form name="Form">
    >
    <textarea rows="10" name="textInput " cols="30"
    wrap="virtual">
    </textarea>
    <textarea rows="10" name="textOutpu t" cols="30"
    wrap="virtual">
    </textarea>
    <p>
    <input type = "button" value="Sort Letters" name="Button"
    onClick="sortva lues(0)">
    <br>
    <input type = "button" value="Sort Numbers" name="Button"
    onClick="sortva lues(1)">
    <br>
    <input type = file name ="file" size =15<br>
    <input type = "button" value="print" name="Button"
    onClick="print( document.Form.t extOutput.value )">
    <br>
    <input type = "reset" value="Reset" name="Button">
    </form>
    </body>
    </html>
    >
    >
    =============== =============== =============== =============== ===
    >
    Thanks in advance for any help!
    In the future, PLEASE post any example code indented with SPACES and not
    tabs.

    While it doesn't have anything to do with your problem, as far as I can
    tell, your HTML markup needs some help as well. Forms should have (and I
    believe) are required to have an action. It is seldom a good idea to
    give multiple form entries the same name.

    Comment

    Working...