user2755667
user2755667

Reputation:

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

Upvotes: 0

Views: 183

Answers (5)

Jeremiah McCurdy
Jeremiah McCurdy

Reputation: 612

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.

Upvotes: 0

Mike Christensen
Mike Christensen

Reputation: 91618

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;

Upvotes: 1

user240141
user240141

Reputation:

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?

Upvotes: 0

McAden
McAden

Reputation: 13972

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

Upvotes: 1

David
David

Reputation: 218847

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.

Upvotes: 0

Related Questions