# Why this code doesn't provide expect result. please help

**URL:** <https://forum.freecodecamp.org/t/why-this-code-doesnt-provide-expect-result-please-help/496285>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [February 9, 2022, 8:09am UTC](https://forum.freecodecamp.org/t/why-this-code-doesnt-provide-expect-result-please-help/496285 "2022-02-09T08:09:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sumitjha85](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/sumitjha85/32/223447_2.png) [@sumitjha85](https://forum.freecodecamp.org/u/sumitjha85)\
**Post date:** [February 9, 2022, 8:09am UTC](https://forum.freecodecamp.org/t/why-this-code-doesnt-provide-expect-result-please-help/496285/1 "2022-02-09T08:09:55Z")

</div>

```auto
/* 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` . */

function titleCase(str) {

  const newTitle = str.split(" ");

  const updatedTitle = [];

  for (let st in newTitle) {

    updatedTitle[st] = newTitle[st]

      .toLowerCase()

      .replace(newTitle[st].charAt(0), newTitle[st].charAt(0).toUpperCase());

  }

  console.log(updatedTitle.join(" "));

  return updatedTitle.join(" ");

}

```

---

<div class="post-metadata">

**Author:** ![lasjorg](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lasjorg/32/280761_2.png) [@lasjorg](https://forum.freecodecamp.org/u/lasjorg)\
**Post date:** [February 9, 2022, 8:30am UTC](https://forum.freecodecamp.org/t/why-this-code-doesnt-provide-expect-result-please-help/496285/2 "2022-02-09T08:30:13Z")

</div>

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (`</>`) to add backticks around text.

 ![Pre-formatted-text](https://global.discourse-cdn.com/freecodecamp/original/3X/1/b/1b18854f1059217f29370444396145cc3a0cd8ae.gif)

See [this post](https://forum.freecodecamp.org/t/forum-code-formatting/25574) to find the backtick on your keyboard.  
**Note:** Backticks (`) are not single quotes (’).

* * *

BTW, If you have a question about a specific challenge as it relates to your written code for that challenge, just click the **Ask for Help** button located on the challenge. It will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.

Or provide the challenge link when asking a question.

- [Title Case a Sentence](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-algorithm-scripting/title-case-a-sentence)

---

<div class="post-metadata">

**Author:** ![kevinSmith](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kevinsmith/32/72934_2.png) [@kevinSmith](https://forum.freecodecamp.org/u/kevinSmith)\
**Post date:** [February 9, 2022, 8:53am UTC](https://forum.freecodecamp.org/t/why-this-code-doesnt-provide-expect-result-please-help/496285/3 "2022-02-09T08:53:39Z")

</div>

I agree with what lasjorg said. And now that we have a link to the challenge…

When in doubt, log things out. A big part of being a good coder is being a good debugger. And being a good debugger is being a good detective.

If I add some log statements, breaking apart your logic a bit, I get:

```auto
function titleCase(str) {
  const newTitle = str.split(" ");
  const updatedTitle = [];

  for (let st in newTitle) {
    console.log(st, newTitle[st])

    const loweredStr = newTitle[st].toLowerCase()
    console.log('lowered', loweredStr)
    console.log('want to replace', newTitle[st].charAt(0), 'with', newTitle[st].charAt(0).toUpperCase(), 'in', loweredStr)
    const titledStr = loweredStr
      .replace(newTitle[st].charAt(0), newTitle[st].charAt(0).toUpperCase());
    console.log('titled', titledStr)
    updatedTitle[st] = titledStr
  }

  return updatedTitle.join(" ");
}
console.log(titleCase("I'm a little tea pot"));

```

I get this output:

> 0 I’m  
> lowered i’m  
> want to replace I with I in i’m  
> titled i’m  
> 1 a  
> lowered a  
> want to replace a with A in a  
> titled A  
> 2 little  
> lowered little  
> want to replace l with L in little  
> titled Little  
> 3 tea  
> lowered tea  
> want to replace t with T in tea  
> titled Tea  
> 4 pot  
> lowered pot  
> want to replace p with P in pot  
> titled Pot  
> i’m A Little Tea Pot

Can you see the issue? It’s there, if you read closely enough.

Another technique is to figure out which ones are working and which ones aren’t. What do the words that are title cased incorrectly have in common? Maybe try some other sentences to see if you can see a pattern.

Work with that for a while and see if you can figure it out. Check back if you can’t find it and we’ll see if we can give a better hint.

---

<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:** [August 10, 2022, 8:54pm UTC](https://forum.freecodecamp.org/t/why-this-code-doesnt-provide-expect-result-please-help/496285/4 "2022-08-10T20:54:26Z")

</div>

Closed on August 10, 2022, 8:54pm UTC

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