Title Case a Sentence - JavaScript Solution & Walkthrough

(11/16) Learn how to solve coding challenges using FreeCodeCamp's curriculum.

Title Case a Sentence - JavaScript Solution & Walkthrough

11/16 Title Case a Sentence

Return the provided string with the first letter of each word capitalized. Make sure the rest of the word is in lower case.

For the purpose of this exercise, you should also capitalize connecting words like the and of.

function titleCase(str) {
  return str;
}

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

Credit: FreeCodeCamp.org

Understanding the Challenge

The "Title Case a Sentence" challenge requires that you write a function that takes a string as an argument and return the string with the first letter of each word capitalized.

For example, the string "I love JavaScript." should return "I Love Javascript."

You are also informed to make sure that the rest of the word is in lowercase. So in the example, note that JavaScript was returned as Javascript. The (S) in the initial string was converted to lowercase (s).

Pseudocode

Given a string
  Convert the string to lowercase
  split the string to an array of the individual words 
  capitalize the first letter of each word
  join the string
return the resulting string.

Solving the Challenge

First, we will convert the string to lower case. Let's save the result to a variable called lowerCase

cosnt lowerCase = str.toLowerCase();
console.log(lowerCase) // i'm a little tea pot

The next step is to split the string. We want to get an array of the individual words. Therefore, we will pass space " " as an argument to the .split(" ") method. Let's save this to a variable called splitString

const splitString = lowerCase.split(" ");
console.log(splitString) // [ 'i\'m', 'a', 'little', 'tea', 'pot' ]

Now, we can use the .map() method to loop through the array of the individual words.

const capitalizeFirst = splitString.map(word => {
    return word[0].toUpperCase() + word.substring(1, word.length)
  })

For each word, we are doing two things.

word[0].toUpperCase()

First we are converting the first letter to upper case.

word.substring(1, word.length)

And secondly, we are concatenating it to the remaining letters in the string. The .substring() is what is helping us get the remaining letters. It extracts parts of a string using the first argument as starting point and the second arguments as end point.

In this case, the start point is index 1 and the end poing is word.length which is the end of the string. Thus, it is going to extract all the remaining letters of the word without the first letter.

Finally we join the string. Note that we're passing " " as an argument to the .join(" ") method.

const finalString = capitalizeFirst.join(" ")

Our function is now complete!

Final Solution

function titleCase(str) {
  const lowerCase = str.toLowerCase();
  const splitString = lowerCase.split(' ');
  const capitalizeFirst = splitString.map(word => {
    return word[0].toUpperCase() + word.substring(1, word.length)
  })

  const finalString = capitalizeFirst.join(' ')
  return finalString;
}

titleCase("I'm a little tea pot"); // I'm A Little Tea Pot

Congratulations!

You just cracked the 11th challenge in this series.

congratulations 001.gif

Cheers and happy coding!