Basic Algorithm Scripting - Title Case a Sentence

Tell us what’s happening:
I’m trying to use string.replace() to find the character after the space, capitalize it and put it back in. It works great except in cases where there is a double letter it “tt” or “pp” and I get this odd placement of the capitalized letter.

Here is my output: I’m A LiTtle tea Pot

Why is the “T” getting put in the wrong place???

Describe your issue in detail here.

Your code so far*
function titleCase(str) {
let newStr = str.toLowerCase()
.replace(/\S/, str[0].toUpperCase())

for (let i = 0; i < newStr.length; i++) {
newStr = newStr.replace(newStr[i+1], newStr[i+1].toUpperCase());
return newStr;


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/ Safari/537.36

Challenge: Basic Algorithm Scripting - Title Case a Sentence

Link to the challenge:

So the way you are doing this is just going to find the first occurrence of the letter in question, and capitalize it. That is how regexes work in this situation if I am not mistaken. You have gotten lucky with the rest in that they are the first letter of their kind in the string.

I’m actually not sure about what I posted earlier, but what it looks like is that the program is just capitalizing the first occurrence of T that it finds. I would suggest testing your code with this in mind and seeing what you find.

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