SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: Help please!

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Apr 2011
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help please!

    Hi everyone,
    can anyone help me with my code, i am having a hard time trying to make the first letter of a string a capital, and the rest lowercase... here is my code

    <script type="text/javascript">
    var input= prompt("Please input a word.");
    var thelength= input.length;
    var test="";
    for(var i=0; i<thelength; i++){
    var num1 = Math.floor(Math.random() * input.length);
    test += input.substring(num1,num1+1);
    }
    // here is where i am having my problem, i want my output to be first character UpperCase and the rest of the output LowerCase. I only thought it has something to to with the substr method but i dunno thx!
    InitChar=test.substr(0,1).toUpperCase();


    </script>

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,191
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    You could simply use something like

    Code JavaScript:
    String.prototype.capitalize = function() {
        return this.charAt(0).toUpperCase() + this.slice(1);
    }
     
    alert(input.capitalize());

    If you want all the first letters of a word to be capital you could use this

    Code JavaScript:
    String.prototype.capitalize = function() {
        return this.charAt(0).toUpperCase() + this.slice(1);
    }
     
    var input = prompt('Please input a word');
     
    if (input && input.length > 0) {
        var temp = input.split(' '), input = [];
     
        for (var i = 0; i < temp.length; i++) {
            input[i] = temp[i].capitalize();
        }
     
        alert(input.join(' '));
    }

  3. #3
    SitePoint Member
    Join Date
    Apr 2011
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    perfect, thx! can you show me however what is wrong with the following code?

    var output=test.charAt(0).toUpperCase() + test.slice(1).toLowerCase();
    alert(testtest);

    i am trying to avoid functions and prototype just for learnings sake. I want to use a code similar to the above with a variable. so the first part test.charAt(0).touppercase() makes the first letter uppercase... and the second part is supposed to make it lowercase? what is wrong with that code?

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,696
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by javascriptman View Post
    can you show me however what is wrong with the following code?

    var output=test.charAt(0).toUpperCase() + test.slice(1).toLowerCase();
    alert(testtest);
    Are you alerting the wrong variable?
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


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
  •