# Basic Algorithm Scripting - Title Case a Sentence

**URL:** <https://forum.freecodecamp.org/t/basic-algorithm-scripting-title-case-a-sentence/581087>\
**Category:** Curriculum Help\
**Created:** [December 28, 2022, 3:31pm UTC](https://forum.freecodecamp.org/t/basic-algorithm-scripting-title-case-a-sentence/581087 "2022-12-28T15:31:45Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mahassan](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/mahassan/32/108336_2.png) [@mahassan](https://forum.freecodecamp.org/u/mahassan)\
**Post date:** [December 28, 2022, 3:31pm UTC](https://forum.freecodecamp.org/t/basic-algorithm-scripting-title-case-a-sentence/581087/1 "2022-12-28T15:31:45Z")

</div>

**Tell us what’s happening:**  
Following is my code but it is not passing the test, I did `console.log` but seems fine then what is wrong?

**Your code so far**

```javascript
function titleCase(str) {
  let wordsSplit = str.split(" ");
  let capitalized = []
  wordsSplit.map((word)=>{
    const capital = `${word[0].toUpperCase()}${word.slice(1).toLowerCase()} ` 
    capitalized.push(capital)
  })
    return capitalized.join('')
}
titleCase("I'm a little tea pot");

```

**Your browser information:**

User Agent is: `Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36`

**Challenge:** Basic Algorithm Scripting - Title Case a Sentence

**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:** ![Lego\_My\_Eggo](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lego_my_eggo/32/134324_2.png) [@Lego\_My\_Eggo](https://forum.freecodecamp.org/u/Lego_My_Eggo)\
**Post date:** [December 28, 2022, 7:31pm UTC](https://forum.freecodecamp.org/t/basic-algorithm-scripting-title-case-a-sentence/581087/3 "2022-12-28T19:31:00Z")

</div>

This problem is hard to spot. Try putting `'--' + titleCase("I'm a little tea pot") + '--'` in your console.log.

---

<div class="post-metadata">

**Author:** ![mahassan](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/mahassan/32/108336_2.png) [@mahassan](https://forum.freecodecamp.org/u/mahassan)\
**Post date:** [December 28, 2022, 9:42pm UTC](https://forum.freecodecamp.org/t/basic-algorithm-scripting-title-case-a-sentence/581087/4 "2022-12-28T21:42:25Z")

</div>

Now I did the following, but didn’t find anything abnormal

```auto
function titleCase(str) {
  let wordsSplit = str.split(" ");
  let capitalized = []
  wordsSplit.map((word)=>{
    const capital = `${word[0].toUpperCase()}${word.slice(1).toLowerCase()} ` 
    capitalized.push(capital)
  })
    return capitalized.join('')
}

console.log('---'+titleCase("I'm a little tea pot")+'---')

```

---

<div class="post-metadata">

**Author:** ![Lego\_My\_Eggo](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lego_my_eggo/32/134324_2.png) [@Lego\_My\_Eggo](https://forum.freecodecamp.org/u/Lego_My_Eggo)\
**Post date:** [December 28, 2022, 11:21pm UTC](https://forum.freecodecamp.org/t/basic-algorithm-scripting-title-case-a-sentence/581087/5 "2022-12-28T23:21:54Z")

</div>

Im getting `---I'm A Little Tea Pot ---` with that. Try highlighting from the end and tell me if you see anything extra that probably should not be there.

---

<div class="post-metadata">

**Author:** ![mahassan](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/mahassan/32/108336_2.png) [@mahassan](https://forum.freecodecamp.org/u/mahassan)\
**Post date:** [December 29, 2022, 12:29am UTC](https://forum.freecodecamp.org/t/basic-algorithm-scripting-title-case-a-sentence/581087/6 "2022-12-29T00:29:04Z")

</div>

Got it but when I remove the ending space, all words are next to each other without any spaces like

```auto
---I'mALittleTeaPot---

```

---

<div class="post-metadata">

**Author:** ![Lego\_My\_Eggo](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lego_my_eggo/32/134324_2.png) [@Lego\_My\_Eggo](https://forum.freecodecamp.org/u/Lego_My_Eggo)\
**Post date:** [December 29, 2022, 1:07am UTC](https://forum.freecodecamp.org/t/basic-algorithm-scripting-title-case-a-sentence/581087/7 "2022-12-29T01:07:08Z")

</div>

You are already using an array method in your code that can help you put those spaces back in. Google some of those methods you are using and hopefully the examples should show you one minor change to fix that.

You could also come up with a way to check if you do or do not need a space added yourself.

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/freecodecamp/original/3X/e/1/e1c433102e34b46dfaa3e15d13ab9a514b127078.svg) [@system](https://forum.freecodecamp.org/u/system)\
**Post date:** [June 29, 2023, 1:07pm UTC](https://forum.freecodecamp.org/t/basic-algorithm-scripting-title-case-a-sentence/581087/8 "2023-06-29T13:07:21Z")

</div>

Closed on June 29, 2023, 1:07pm UTC

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.
