Title Case Algorithm Challenge

Title Case Algorithm Challenge
0

#1

Hey fellow Campers. I’m working on the Title Case Algorithm Challenge. Well, more accurately, I peeked at the solution out of frustration, and now I’m just trying to understand why mine didn’t work.

function titleCase(str) {
  var words = str.toLowerCase().split(" ");
  for(var i=0; i<words.length; i++){
    words[i][0].toUpperCase();
    words.join(" "); 
  }
  return words;
}

titleCase("I'm a little tea pot");

Any feedback on why this doesn’t pass muster?


#2

This does nothing. .toUpperCase returns a new string representing the calling string converted to upper case. It does not modify the original string.

You can do this:

    var firstLetterOfWord = words[i][0].toUpperCase();
    console.log(firstLetterOfWord); // displays the upper case version of the 1st letter in each word

Then you can use firstLetterOfWord and combine it with the remainder of the word and assign it back to words[i] before joining the words back together.


#3

Thanks for the reply.

So just so I’m clear on this, the “calling string” would be whatever comes before .toUpperCase()?
E.g. - callingString.toUpperCase()?

So, I have to assign the function call to a variable in order to make it so I can work with it? Am I understanding you correctly?


#4

Yes, you are correct.