FCC ‘Title Case A Sentence’

Today we’re going to work through another of freeCodeCamp’s Basic Algorithm Scripting challenges – ‘Title Case A Sentence’.  To solve this challenge we must write a function that accepts a string, then returns that string with the first letter of each word capitalized and the remainder of each word in lowercase.

We’ll be using concepts that we’ve covered in previous posts (i.e. for loops, string methods, and arrays) and adding a couple new things like method chaining and some new string methods.

As I’ve mentioned previously, in JavaScript, the data type String has many methods associated with it.  W3schools has a good intro to these methods, MDN covers the string object on a deeper level.

Here’s the starting point given to us by freeCodeCamp.

We’ll start solving this challenge by breaking the given string (str) into an array of words, the same way we started ‘Find the Longest Word in a String’.

Quick tangent – we haven’t discussed yet that JavaScript can be tested right in your browser.  If you have a question about what a snippet of code does, just plug it into the console of your browser and test it.   To make sure the str.split() method that I just created is doing what I want, I plugged it into the console in Chrome and tested it.

As you can see, str.split(‘ ‘) is splitting the given string into an array, with each word as an individual element.  Now I’m confident in continuing with the solution to the algorithm challenge…

Next, we need to iterate through the array and capitalize the first letter of each word and lowercase the rest of the word.  To iterate through the array, we’ll use a for loop.  To handle the upper/lowercasing of the words, we’ll need a couple new string methods.

For a deeper understanding, and a list of many more string methods, check out the MDN page on strings.  Today, we’ll be using charAt(), toUpperCase(), toLowerCase() and slice().

toUpperCase() and toLowerCase() do exactly what they sound like, they return the calling string value in upper and lower cases respectively.  charAt() allows you to access a specific character of a string and takes one parameter – the index of the character you want to access (indexes of strings start at 0, just like arrays).

slice() allows you to access a specific segment of a string.  It takes two arguments, the indexes of where you want the segment to start and finish.  The second parameter is optional, if you leave it out, the slice will start at the first index you provide and continue to the end of the string.  Very important – all four methods mentioned above return the value of a string, they do not change the calling string.  See code below for demonstration…

Now, as mentioned above, we can chain these methods together using dot notation, so that after one method is called, a second method is called on the returned string of the first.

Check out this post on method chaining for a better understanding.

We can use everything we’ve talked about to get the first character of a string and capitalize it, then get the rest of the string (starting at index 1) and lowercase it.  Putting it all together looks like this:

Several notes on the above solution…

We used the join() method, which does the exact opposite of split.  join() is an array method that returns a string delimited by what you pass as an argument.  In this case, we gave an empty space (” “) as the argument, so each element of the array was joined together separated by a space.

Also, you will note that we used a ‘+’ to connect 2 parts of a string together.  This is an easy way in JavaScript to build a string.  It can be used with both literal strings and variables as such:

Hopefully this straightened out the code for you…  See you next time.

-Jeremy