Solving "Title Case a Sentence" / freeCodeCamp Algorithm Challenges 
Tags: freecodecamp, algorithms, challenge, javascript
Published: 2020-05-28 17:29:49

---
title: Solving "Title Case a Sentence" / freeCodeCamp Algorithm Challenges 
published: true
description: My guide, notes, and solution to freeCodeCamp's basic algorithm challenge, "Title Case a Sentence" 
tags: #freeCodeCamp, #algorithms, #challenge, #javascript
canonical_url: https://virenb.cc/fcc-011-title-case-a-sentence
series: Solving freeCodeCamp's Algorithm Challenges
---

![Title Case a Sentence](//images.ctfassets.net/yzwpq1epaq68/6jC78Q3j6vf2chg849pJOr/134cbd3ebeb042bf29b2b592ef9f81b0/ss-011.png)

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

### Starter Code

```javascript
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()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split "MDN Docs: 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()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toLowerCase "MDN Docs: 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()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toUpperCase "MDN Docs: 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()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring "substring docs on MDN")

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]


<details>
  <summary>**[SPOILER: SOLUTION TO CODE BELOW]**</summary>
 	
```javascript
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:

```javascript
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(' ');
}
```
 
</details>

---

## Links & Resources

['Title Case a Sentence' Challenge on fCC](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-algorithm-scripting/title-case-a-sentence "Basic Algorithm Scripting: Title Case a Sentence Challenge on fCC Website")

[freeCodeCamp](https://www.freecodecamp.org/ "freeCodeCamp")

[Donate to fCC!](https://www.freecodecamp.org/donate "fCC Donation Page")

[Solution on my GitHub](https://github.com/virenb/fcc-algorithms/blob/master/011---title-case-a-sentence.js "GitHub Solution")

Thank you for reading!