loading...

Solving "Title Case a Sentence" / freeCodeCamp Algorithm Challenges

virenb profile image Viren B Originally published at virenb.cc ・3 min read

Solving freeCodeCamp's Algorithm Challenges (20 Part Series)

1) FCC Algorithm Challenges / Convert Celsius to Fahrenheit 2) FCC Algorithm Challenges / Reverse a String 3 ... 18 3) FCC Algorithm Challenges / Factorialize a Number 4) FCC Algorithm Challenges / Find the Longest Word in a String 5) FCC Algorithm Challenges / Return Largest Numbers in Arrays 6) Solving "Confirm the Ending" / FreeCodeCamp Algorithm Challenges 7) Solving "Repeat a String Repeat a String" / FreeCodeCamp Algorithm Challenges 8) Solving "Truncate a String" / freeCodeCamp Algorithm Challenges 9) Solving "Finders Keepers" / freeCodeCamp Algorithm Challenges 10) Solving "Boo who" / freeCodeCamp Algorithm Challenges 11) Solving "Title Case a Sentence" / freeCodeCamp Algorithm Challenges 12) Solving "Slice and Splice" / freeCodeCamp Algorithm Challenges 13) Solving "Falsy Bouncer" / freeCodeCamp Algorithm Challenges 14) Solving "Where do I Belong" / freeCodeCamp Algorithm Challenges 15) Solving "Mutations" / freeCodeCamp Algorithm Challenges 16) Solving "Chunky Monkey" / freeCodeCamp Algorithm Challenges 17) Solving "Sum All Numbers in a Range" / freeCodeCamp Algorithm Challenges 18) Solving "Diff Two Arrays" / freeCodeCamp Algorithm Challenges 19) Solving "Seek and Destroy" / freeCodeCamp Algorithm Challenges 20) Solving "Wherefore art thou" / 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!

Solving freeCodeCamp's Algorithm Challenges (20 Part Series)

1) FCC Algorithm Challenges / Convert Celsius to Fahrenheit 2) FCC Algorithm Challenges / Reverse a String 3 ... 18 3) FCC Algorithm Challenges / Factorialize a Number 4) FCC Algorithm Challenges / Find the Longest Word in a String 5) FCC Algorithm Challenges / Return Largest Numbers in Arrays 6) Solving "Confirm the Ending" / FreeCodeCamp Algorithm Challenges 7) Solving "Repeat a String Repeat a String" / FreeCodeCamp Algorithm Challenges 8) Solving "Truncate a String" / freeCodeCamp Algorithm Challenges 9) Solving "Finders Keepers" / freeCodeCamp Algorithm Challenges 10) Solving "Boo who" / freeCodeCamp Algorithm Challenges 11) Solving "Title Case a Sentence" / freeCodeCamp Algorithm Challenges 12) Solving "Slice and Splice" / freeCodeCamp Algorithm Challenges 13) Solving "Falsy Bouncer" / freeCodeCamp Algorithm Challenges 14) Solving "Where do I Belong" / freeCodeCamp Algorithm Challenges 15) Solving "Mutations" / freeCodeCamp Algorithm Challenges 16) Solving "Chunky Monkey" / freeCodeCamp Algorithm Challenges 17) Solving "Sum All Numbers in a Range" / freeCodeCamp Algorithm Challenges 18) Solving "Diff Two Arrays" / freeCodeCamp Algorithm Challenges 19) Solving "Seek and Destroy" / freeCodeCamp Algorithm Challenges 20) Solving "Wherefore art thou" / freeCodeCamp Algorithm Challenges

Posted on May 28 by:

virenb profile

Viren B

@virenb

Self-taught developer, always looking to learn more.

Discussion

markdown guide