What's wrong with my code its returning "I'M A Little Tea Pot" instead of "I'm A Little Tea Pot'

Tell us what’s happening:
Describe your issue in detail here.

  **Your code so far**

function titleCase(str) {
let a = str.toLowerCase();
let b = /\b[a-z]/g;
return a.replace(b, b => b.toUpperCase());

}

console.log(titleCase("I'm a little tea pot"));
  **Your browser information:**

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

Challenge: Title Case a Sentence

Link to the challenge:

It’s cool to try and use a regex solution for this, but this isn’t going to work as written. To see the problem, see how your regex is seeing your sentence. Try seeing the results of this:

function titleCase(str) {
  let a = str.toLowerCase();
  let b = /\b[a-z]/g;
  console.log('split', a.match(b))
  return a.replace(b, b => b.toUpperCase());
}

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

That is how your regex is finding its word boundaries. Can you see the problem?

I did the same thing but somehow it selects the apostrophe and yes dis solution also shows the same result

I know it shows the same result. I didn’t change your solution, I just inserted a log statement so you could see the problem. The point was that it things the “m” is the start of a new word.

So do u have any idea on how to exclude the apostrophe

Not off the top of my head. You may have to try a different approach. I know this can be solved with regex but can also be solved with regex.

This happens in coding sometimes, you try an approach that fails, so you take a step back and see if you need rethink your approach.

1 Like

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