DEV Community

Viren B
Viren B

Posted on • Originally published at virenb.cc

Solving "Title Case a Sentence" / freeCodeCamp Algorithm Challenges

Title Case a Sentence

Let us solve freeCodeCamp's Basic Algorithm Challenge, "Title Case a Sentence"

Starter Code

function titleCase(str) {
  return str;
}

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

Tests

titleCase("I'm a little tea pot") should return a string.
titleCase("I'm a little tea pot") should return I'm A Little Tea Pot.
titleCase("sHoRt AnD sToUt") should return Short And Stout.
titleCase("HERE IS MY HANDLE HERE IS MY SPOUT") should return Here Is My Handle Here Is My Spout.

Instructions

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".


Read

  • Our input is a string of multiple characters, usually phrases or sentences (one or more words).
  • We must ouput a string, changing the capitalizations of the first letter of each word in the string.
  • There are a few methods on the String type which we can utilize but we will have to loop through each word within the string, str.

Thoughts

The two things which we must accomplish are to access each word within the str, and alter the capitalizations.

Since we have to access each word in str, my first instinct is to call split(' ) in order to convert str from a string to an array. We would then be able to loop through the array, accessing each index (word).

"sHoRt AnD sToUt".split(' ');

Result: [ "sHoRt", "AnD", "sToUt" ]

MDN Documentation: split()

Now, we want to capitalize the first letter in each word, and ensure the rest are lowercased.

Looking at the test cases, we are dealing with different capitalizations in each test:

i.e. [ "sHoRt", "AnD", "sToUt" ]

We can use a built-in method, toLowerCase() to ensure we are working with all lowercased characters.

"sHoRt AnD sToUt".toLowerCase();

Result: "short and stout"

MDN Documentation: toLowerCase()

Our words are now all lowercased, we can focus on trying to capitalize the first character of each word. We want to focus on the 0th index of the string (the first character).

We can call toUpperCase() on the first character.

MDN Documentation: toUpperCase()

How to handle the rest of the word though?

There is another built-in method, substring() which we can use.

str.substring(indexStart[, indexEnd]) -- the indexEnd being optional

Since we are capitalizing the [0] index of the word, we want to take the substring from index [1].

"hello".substring(1)
Result: "ello"

MDN Documentation: substring()

So to put it all together, we will have to lowercase the strings with toLowerCase(), split it into an array with split(), perform a loop on the array, taking each item, capitalize the first letter with toUpperCase() and add the rest of the string with substring().

For the looping, a for loop would work, as well as a method such as map().

Some pseudo pseudocode:

function titleCase(str) {
    make str all lower case
    split string into an array

    loop through array, run array.length times
        each loop: array item 0th index.toUpperCase() + array item 1st index.substring();

    join array back into a string
    return string   
}

Solving [SPOILER: CONTAINS SOLUTION]

[SPOILER: SOLUTION TO CODE BELOW]

function titleCase(str) {
   return str.toLowerCase().split(' ')
    .map(word => word[0].toUpperCase() + word.substring(1))
    .join(' ')
}

Alternate Solution, using a new array and for loop:

function titleCase(str) {
  let newArr = [];
  let strArr = str.toLowerCase().split(' ');
  for (let i = 0; i < strArr.length; i++) {
    newArr.push(strArr[i][0].toUpperCase() + strArr[i].substring(1));
  }
  return newArr.join(' ');
}

Links & Resources

'Title Case a Sentence' Challenge on fCC

freeCodeCamp

Donate to fCC!

Solution on my GitHub

Thank you for reading!

Top comments (1)

Collapse
 
saifrehman99 profile image
Saif ur Rehman

Here's a one line code :D

"I'm a little tea pot".split(' ').map(x => x[0].toUpperCase() + x.slice(1).toLowerCase()).join(' ')