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

/* 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]


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


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

  return updatedTitle.join(" ");


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.

See this post 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.

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:

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.

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