Simple JavaScript question

Discussion in 'Javascript' started by paul.denlinger@gmail.com, Feb 17, 2007.

  1. Guest

    Please excuse this very basic question; I'm just starting to learn
    JavaScript.

    I have just modified added a toUpperCase statement to change strings
    to upper case, but it does not run.

    Can you please tell me what I have done wrong? I have pasted the code
    in below.

    Thank you.

    -------------------------------------------------------------------------------------------------

    // JavaScript Document

    // initialize the counter and the array
    var numnames=0;
    var names = new Array();
    function SortNames() {
    // Get the name from the text field
    SortNames toUpperCase();
    //Change the names to all upper case
    thename=document.theform.newname.value;
    // Add the name to the array
    names[numnames]=thename;
    // Increment the counter
    numnames++;
    // Sort the array
    names.sort();
    document.theform.sorted.value=names.join("\n");
     
    , Feb 17, 2007
    #1
    1. Advertising

  2. Evertjan. Guest

    wrote on 17 feb 2007 in comp.lang.javascript:
    > // JavaScript Document
    >
    > // initialize the counter and the array
    > var numnames=0;
    > var names = new Array();
    > function SortNames() {
    > // Get the name from the text field
    > SortNames toUpperCase();


    The name of a function cannot return a string in itself,
    [unless you programme recursive, which is not here]

    toUpperCase() should be attached by a period like this:

    var a = 'A StrinG';
    alert( a.toUpperCase(); ); // shows: A STRING


    > //Change the names to all upper case
    > thename=document.theform.newname.value;


    This could work, if the form is correctly named

    better keep your variables local, and do this:

    var thename =
    document.forms['theform'].elements['newname'].value;


    > // Add the name to the array
    > names[numnames]=thename;
    > // Increment the counter
    > numnames++;


    You forgot to define a loop, this way only names[0]
    is created and assigned

    > // Sort the array
    > names.sort();
    > document.theform.sorted.value=names.join("\n");


    you will need to end the function with a }

    ============

    If, Paul, I did not misread your intended purpose,
    perhaps the tested code below will help you:

    ============= test.html ======================
    <script type='text/javascript'>

    function SortNames() {
    var names = new Array();
    var f = document.forms['theForm'];
    for (var n=0;n<3;n++)
    names[n] =
    f.elements['elem' + n].value.toUpperCase();
    f.elements['sorted'].value =
    names.sort().join("+");
    return false;
    };

    </script>


    <form name='theForm' onsubmit='return SortNames();'>
    <input name='elem0' value='xyz x'><br>
    <input name='elem1' value='ABC x'><br>
    <input name='elem2' value='Pqr x'><br>
    <input type='' name='sorted' value=''><br>
    <input type='submit' value='Sort me'><br>
    </form>
    =================================================

    Are you sure I am not making your school assignment?


    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., Feb 17, 2007
    #2
    1. Advertising

  3. Guest

    On Feb 17, 11:28 am, "Evertjan." <>
    wrote:
    > wrote on 17 feb 2007 in comp.lang.javascript:
    >
    > > // JavaScript Document

    >
    > > // initialize the counter and the array
    > > var numnames=0;
    > > var names = new Array();
    > > function SortNames() {
    > > // Get the name from the text field
    > > SortNames toUpperCase();

    >
    > The name of a function cannot return a string in itself,
    > [unless you programme recursive, which is not here]
    >
    > toUpperCase() should be attached by a period like this:
    >
    > var a = 'A StrinG';
    > alert( a.toUpperCase(); ); // shows: A STRING
    >
    > > //Change the names to all upper case
    > > thename=document.theform.newname.value;

    >
    > This could work, if the form is correctly named
    >
    > better keep your variables local, and do this:
    >
    > var thename =
    > document.forms['theform'].elements['newname'].value;
    >
    > > // Add the name to the array
    > > names[numnames]=thename;
    > > // Increment the counter
    > > numnames++;

    >
    > You forgot to define a loop, this way only names[0]
    > is created and assigned
    >
    > > // Sort the array
    > > names.sort();
    > > document.theform.sorted.value=names.join("\n");

    >
    > you will need to end the function with a }
    >
    > ============
    >
    > If, Paul, I did not misread your intended purpose,
    > perhaps the tested code below will help you:
    >
    > ============= test.html ======================
    > <script type='text/javascript'>
    >
    > function SortNames() {
    > var names = new Array();
    > var f = document.forms['theForm'];
    > for (var n=0;n<3;n++)
    > names[n] =
    > f.elements['elem' + n].value.toUpperCase();
    > f.elements['sorted'].value =
    > names.sort().join("+");
    > return false;
    >
    > };
    >
    > </script>
    >
    > <form name='theForm' onsubmit='return SortNames();'>
    > <input name='elem0' value='xyz x'><br>
    > <input name='elem1' value='ABC x'><br>
    > <input name='elem2' value='Pqr x'><br>
    > <input type='' name='sorted' value=''><br>
    > <input type='submit' value='Sort me'><br>
    > </form>
    > =================================================
    >
    > Are you sure I am not making your school assignment?
    >
    > --
    > Evertjan.
    > The Netherlands.
    > (Please change the x'es to dots in my emailaddress)


    Sorry I did not give you the URL for the HTML page; it's
    http://www.china-ready.com/jstest/arraysorting.htm Hope this makes
    what I'm trying to do clearer.

    Actually, no I'm not doing a school assignment; this is entirely self-
    imposed. I'm working my way thru Sams Teach Yourself Javascript in 24
    Hours. This is the exercise at the end of Hour 5.

    If you have any suggestions about how to use Firebug to debug
    JavaScript; I'd appreciate your input. I'm so new that I can't figure
    out the debug messages.

    Thanks again!

    Paul
     
    , Feb 17, 2007
    #3
  4. Evertjan. Guest

    wrote on 17 feb 2007 in comp.lang.javascript:

    >> If, Paul, I did not misread your intended purpose,
    >> perhaps the tested code below will help you:
    >>
    >> ============= test.html ======================
    >> <script type='text/javascript'>
    >>
    >> function SortNames() {
    >> var names = new Array();
    >> var f = document.forms['theForm'];
    >> for (var n=0;n<3;n++)
    >> names[n] =
    >> f.elements['elem' + n].value.toUpperCase();
    >> f.elements['sorted'].value =
    >> names.sort().join("+");
    >> return false;
    >>
    >> };
    >>
    >> </script>
    >>
    >> <form name='theForm' onsubmit='return SortNames();'>
    >> <input name='elem0' value='xyz x'><br>
    >> <input name='elem1' value='ABC x'><br>
    >> <input name='elem2' value='Pqr x'><br>
    >> <input type='' name='sorted' value=''><br>
    >> <input type='submit' value='Sort me'><br>
    >> </form>
    >> =================================================
    >>
    >> Are you sure I am not making your school assignment?
    >>
    >> --
    >> Evertjan.
    >> The Netherlands.
    >> (Please change the x'es to dots in my emailaddress)

    >
    > Sorry I did not give you the URL for the HTML page; it's
    > http://www.china-ready.com/jstest/arraysorting.htm Hope this makes
    > what I'm trying to do clearer.


    Trying your page, IE shows javascript errors, that you can read and see
    the line number. Debugging those errors is your first line of defence.

    > Actually, no I'm not doing a school assignment; this is entirely self-
    > imposed. I'm working my way thru Sams Teach Yourself Javascript in 24
    > Hours. This is the exercise at the end of Hour 5.


    It seems you should try to understand the code, thoroughly bedore jumping
    to the next. I never used a book for learning JS, as there is so much
    code on the web that you can view source and try out.

    > If you have any suggestions about how to use Firebug to debug


    Never heard of that, is that the standard FF debugger?

    > JavaScript; I'd appreciate your input. I'm so new that I can't figure
    > out the debug messages.


    Debugging in the teaching fase should be done by hand, using small
    snippets of code at a time [modular programming] that you test so tthat
    you are absolutely sure it works as planned.

    Put in alert(aVariable); bedore and after the error line to inspect
    errors and eliminate them one by one.

    IE shows nice error warnings. FF too I am told.

    [in IE you should switch "friendly errors" off, they are vey unfriendly
    while debugging]

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., Feb 17, 2007
    #4
  5. Ian Collins Guest

    wrote:
    >
    > If you have any suggestions about how to use Firebug to debug
    > JavaScript; I'd appreciate your input. I'm so new that I can't figure
    > out the debug messages.
    >

    Just use the JavaScript console and you will see:

    missing ; before statementsort.js (line 8)
    SortNames toUpperCase()(

    The typo is pretty obvious once it has been pointed out!

    --
    Ian Collins.
     
    Ian Collins, Feb 17, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    586
  2. Kevin Spencer

    Re: Simple Simple question!!!

    Kevin Spencer, Jun 25, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    694
    Kevin Spencer
    Jun 25, 2004
  3. Daniel Frey

    Simple Question - Simple Answer?

    Daniel Frey, Dec 28, 2004, in forum: XML
    Replies:
    4
    Views:
    872
    Daniel Frey
    Jan 12, 2005
  4. Oli

    simple simple question

    Oli, Jan 26, 2004, in forum: ASP General
    Replies:
    10
    Views:
    390
    Roland Hall
    Jan 26, 2004
  5. Peter Bailey

    simple, simple array question

    Peter Bailey, Apr 8, 2008, in forum: Ruby
    Replies:
    7
    Views:
    237
    Peter Bailey
    Apr 8, 2008
Loading...

Share This Page