简体   繁体   中英

whats wrong with my capitalize letters in words code?

function LetterCapitalize(str) { 
  return str.charAt(0).toUpperCase()+ str.slice(1);
  var spaceIndex=str.indexOf(' ');
  var first=str.substring(0,spaceIndex);
  var second=str.substring(spaceIndex+1,str.length)

  return LetterCapitalize(first)+" " + LetterCapitalize(second)
}
console.log(LetterCapitalize("hello world"))

not sure what i did wrong but only H in hello is capitalized

When your function is called, the very first thing it's doing is:

return str.charAt(0).toUpperCase()+ str.slice(1);

This returns the first character of the string converted to upper case, plus the rest of the string (as is) starting from index 1.

Since the function returns from there, nothing else in your function is being executed .

How about something like:

function LetterCapitalize(str) { 
    var words = str.split(' '); // create an array of each word
    for(var i = 0; i < words.length; i++) // Loop through each word
        words[i] = words[i].charAt(0).toUpperCase() + words[i].slice(1); // capitalize first character of each word

    return words.join(' '); // join the array back into a string
}

Also, if you're simply trying to do this for display purposes, you can use the CSS: text-transform: capitalize;

See the very first line of code in your function?

It exits your function (returns) and none of the other code in that function is executed. As far as the javascript interpreter is concerned, your code might as well be:

function LetterCapitalize(str) { 
    return str.charAt(0).toUpperCase()+ str.slice(1);
}

thus: Hello world

This function:

function LetterCapitalize(str) { 
    return str.charAt(0).toUpperCase()+ str.slice(1);
    var spaceIndex=str.indexOf(' ');
    var first=str.substring(0,spaceIndex);
    var second=str.substring(spaceIndex+1,str.length)

    return LetterCapitalize(first)+" " + LetterCapitalize(second)
}

is logically equivalent to this function:

function LetterCapitalize(str) { 
    return str.charAt(0).toUpperCase()+ str.slice(1);
}

As soon as you return from the function, nothing else happens in that function. You've returned a value, so it's done. In this case, the value you returned is a single uppercased character plus the rest of the string. Your further attempts to find a second word in the string never get executed.

If you can use CSS its pretty simple:

  <style type="text/css">
    h1.uppercase { text-transform: capitalize}
    </style>
    <h1 class="uppercase">Uppercase or lowercase?</h1>
   Output :** Uppercase Or Lowercase **

Is that what you are looking for?

This is a simple adjustment to the very clean and concise code above from Mike Christensen, who wrote this answer (which happens to be identical to mine in every way but one):

function LetterCapitalize(str) {

var words = str.split(' '); // create an array of each word
for(var i = 0; i < words.length; i++) // Loop through each word
    words[i] = words[i].charAt(0).toUpperCase() + words[i].slice(1); // capitalize first character of each word

return words.join(' '); // join the array back into a string

}

edit: words[i].charAt(0).toUpperCase() #=> words[i][0].toUpperCase()

I only include this version to help others have an idea of the different ways to call the desired character, and maybe help further their understanding of what exactly is happening in the code. I know this helped me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM