Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: Help With This Program Please :)

  1. #1
    Join Date
    Nov 2008
    Location
    USA / Illinois / Morton
    Posts
    8
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Arrow Help With This Program Please :)

    Hi guys,

    Okay... So I'm trying to create a program that prompts for a users name in lower case and then writes the name out with the initial letter of their first and last name capitalized.

    So far I've got this:

    Code:
    <!--
    var test = prompt("Enter your name in lower case");
    
    {
    test = test.substr(0,1).toUpperCase()+test.substr(1);
    document.write(test);
    }
    //-->
    This is all fine but it only works for the first name. I could probably resolve this by prompting for the first and last names seperately and then repeating the above code but that's not exactly what I want. I want it to only prompt once and capitalize their first name, middle name or last name, etc.

    I did a bit of research and the toTitleCase() function kept on popping up, but I don't know if that's an actual function for JavaScript - Or is it?

    Any help on this issue would be greatly appreciated.

    Thank you in advance!

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
       "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script type="text/javascript">
    function pname(){
     var test = prompt('Enter your name in lower case').split(' ');
     for (var i = test.length - 1; i > -1; --i)
      test[i] = test[i].substring(0,1).toUpperCase() + test[i].substring(1);
     document.getElementById('uname').firstChild.nodeValue = test.join(' ');
    };
    window.onload = pname;
    </script>
    </head>
    <body>
    <div>
    <span id="uname">&nbsp;</span>
    </div>
    </body>
    </html>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. The Following User Says Thank You to jscheuer1 For This Useful Post:

    Tseng (11-14-2008)

  4. #3
    Join Date
    Nov 2008
    Location
    USA / Illinois / Morton
    Posts
    8
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default

    Is there no such function called toTitleCase()?

  5. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    None native to most browsers, probably none at all unless someone writes it. Now, this is only as regards javascript, perhaps in other languages . . . But that wouldn't apply here.

    Se also:

    http://www.w3schools.com/jsref/jsref_obj_string.asp
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  6. #5
    Join Date
    Nov 2008
    Location
    USA / Illinois / Morton
    Posts
    8
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default

    Is there any other possible way of creating this program without the bolded lines of code? By that I mean a less complex method. Sorry to be a pain.

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
       "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script type="text/javascript">
    function pname(){
     var test = prompt('Enter your name in lower case').split(' ');
     for (var i = test.length - 1; i > -1; --i)
      test[i] = test[i].substring(0,1).toUpperCase() + test[i].substring(1);
     document.getElementById('uname').firstChild.nodeValue = test.join(' ');
    };
    window.onload = pname;
    </script>
    </head>
    <body>
    <div>
    <span id="uname">&nbsp;</span>
    </div>
    </body>
    </html>

  7. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Sure. However, doing it the way I have outlined is one of the most standards compliant methods available. Why is that a problem? It achieves basically the same result as document.write (which is not recommended), and results in code that can be reused after the page has loaded. Code that uses document.write can never be run after page load in modern browsers without obliterating the existing page.

    Anyways, as I say, yes. What exactly did you have in mind?
    Last edited by jscheuer1; 11-14-2008 at 08:59 AM. Reason: add specificity
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  8. #7
    Join Date
    Nov 2008
    Location
    USA / Illinois / Morton
    Posts
    8
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default

    It's just that this program is for a school project and I need to be able to explain it to my teacher, plus I also need to be able to play around with the program myself to figure it out and know what I'm doing. I would prefer the document.write method as that's the only one we've been taught at the moment. Additionally, the program won't be reused on the page so it will not be a problem using document.write.

    Thank you for the help so far!

  9. #8
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Since it is for school, I'm not going to tell you exactly what to do. I will tell you that:

    HTML Code:
    <span id="uname">&nbsp;</span>
    is just a receiving element for:

    Code:
    document.getElementById('uname').firstChild.nodeValue = test.join(' ');
    So if you are skipping the bold part, you won't need the receiving element. And that:

    Code:
    test.join(' ');
    in the code there returns the results you are looking for in string form.

    I will mention too that, if you are using document.write, you cannot run the code onload as shown in my example. Also that your teacher is getting you off on the wrong foot by even mentioning document.write.

    Maybe you should research nodeValue and any other terms you don't understand (these are all easily found using Google) and surprise your teacher by knowing what it means.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  10. The Following User Says Thank You to jscheuer1 For This Useful Post:

    Tseng (11-14-2008)

  11. #9
    Join Date
    Nov 2008
    Location
    USA / Illinois / Morton
    Posts
    8
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default

    For some reason or another, it keeps returning it with a "," inbetween the words. I cannot figure out why it's doing this... I'd be really grateful if you could resolve this minor problem.

    Code:
    <html>
    <head>
    <title></title>
    <script language = "JavaScript">
    {
    var test = prompt('Enter your name in lower case').split(' ');
    for (var i = test.length - 1; i > -1; --i)
    test[i] = test[i].substring(0,1).toUpperCase() + test[i].substring(1);
    document.write(test);
    }
    </script>
    </head>
    <body>
    </body>
    </html>

  12. #10
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I already stated that for you:

    Quote Originally Posted by jscheuer1 View Post

    Code:
    test.join(' ');
    in the code there returns the results you are looking for in string form.
    so:

    Code:
    document.write(test.join(' '));
    BTW, good job on adapting the rest of it. But you really should place it in the body of the page, and:

    Code:
     language = "JavaScript"
    is deprecated, it should be:

    Code:
     type="text/javascript"
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  13. The Following User Says Thank You to jscheuer1 For This Useful Post:

    Tseng (11-14-2008)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •