# Title Case a Sentence - Error

**URL:** <https://forum.freecodecamp.org/t/title-case-a-sentence-error/288005>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [June 21, 2019, 2:09pm UTC](https://forum.freecodecamp.org/t/title-case-a-sentence-error/288005 "2019-06-21T14:09:45Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ZeroOne](https://avatars.discourse-cdn.com/v4/letter/z/d26b3c/32.png) [@ZeroOne](https://forum.freecodecamp.org/u/ZeroOne)\
**Post date:** [June 21, 2019, 2:09pm UTC](https://forum.freecodecamp.org/t/title-case-a-sentence-error/288005/1 "2019-06-21T14:09:45Z")

</div>

I am a newbie.

Please help me.  
**Tell me what’s happening:**

**Your code so far**

```js

String.prototype.replaceAt = function(index, character) {
    return this.substr(0, index) + character + this.substr(index+character.length);
};

function titleCase(str) {
  var newTitle = str.split('');
  var updatedTitle = [];
  for (var st in newTitle) {
    updatedTitle[st] = newTitle[st].toLowerCase().replaceAt(0, newTitle[st].charAt(0).toUpperCase());

  }
  return updatedTitle.join(' ');
  }

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

/* var newTitle = str.split(' ');
    var updatedTitle = [];
    for (var st in newTitle) {
        updatedTitle[st] = newTitle[st].toLowerCase().replaceAt(0, newTitle[st].charAt(0).toUpperCase());
    }
    return updatedTitle.join(' ');
} */

```

**Your browser information:**

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36`.

**Link to the challenge:**

> **[freeCodeCamp.org](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-algorithm-scripting/title-case-a-sentence)**
>
> Learn to Code — For Free

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [June 21, 2019, 3:09pm UTC](https://forum.freecodecamp.org/t/title-case-a-sentence-error/288005/3 "2019-06-21T15:09:16Z")

</div>

They have created such a method, it is above the `titleCase` function declaration

---

<div class="post-metadata">

**Author:** ![chris-tse](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/chris-tse/32/47141_2.png) [@chris-tse](https://forum.freecodecamp.org/u/chris-tse)\
**Post date:** [June 21, 2019, 3:16pm UTC](https://forum.freecodecamp.org/t/title-case-a-sentence-error/288005/5 "2019-06-21T15:16:48Z")

</div>

> [@ZeroOne](#):
>
> var newTitle = str.split(‘’);

This line here is splitting the input string into individual characters. This means your loop is replacing every single letter instead of the first letter of each word. You will want to split on the space character `str.split(' ')` instead of empty string.

Another note while we’re here, it is generally not a good idea to extend the prototype of a built-in class like `String` with your own methods, but for a challenge like this it’s not a big deal.

---

<div class="post-metadata">

**Author:** ![pgeerkens](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/pgeerkens/32/117727_2.png) [@pgeerkens](https://forum.freecodecamp.org/u/pgeerkens)\
**Post date:** [June 21, 2019, 4:58pm UTC](https://forum.freecodecamp.org/t/title-case-a-sentence-error/288005/6 "2019-06-21T16:58:19Z")

</div>

re

> it is generally not a good idea to extend the prototype of a built-in class like `String` with your own methods

This would seem a good intro to the _ **Cardinal Rule of SW Development** _:

**Maximize cohesion; minimize coupling**

Many _design patterns_ are simply examples of how to avoid naively or carelessly violating this rule.

The reason why one doesn’t _extend the prototype of a built-in class like `String`_ is because it introduces a global coupling to every usage of the **string** type throughout the entire scope of the application.

---

<div class="post-metadata">

**Author:** ![ZeroOne](https://avatars.discourse-cdn.com/v4/letter/z/d26b3c/32.png) [@ZeroOne](https://forum.freecodecamp.org/u/ZeroOne)\
**Post date:** [June 22, 2019, 1:30pm UTC](https://forum.freecodecamp.org/t/title-case-a-sentence-error/288005/7 "2019-06-22T13:30:35Z")

</div>

Thank you very much. Have a good day.

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 25, 2021, 12:36pm UTC](https://forum.freecodecamp.org/t/title-case-a-sentence-error/288005/8 "2021-01-25T12:36:57Z")

</div>

Closed on January 25, 2021, 12:36pm UTC


