Tell us what’s happening:
Describe your issue in detail here.
**Your code so far**
function titleCase(str) {
const regex = /(?<=\s?)\w+\'\w+|(?<=\s?)\w+/ig;
let wordArray = str.match(regex);
let finalArray = [];
let finalStr = "";
for (let i = 0; i < wordArray.length; i++) {
let testArray = "";
testArray = wordArray[i][0].toUpperCase() + wordArray[i].slice(1).toLowerCase();
finalArray.push(testArray)
}
for (let j = 0; j < finalArray.length; j++) {
finalStr += finalArray[j] + " ";
}
let noSpaceStr = finalStr.replace(/((\w+)\s)$/, '$2');
console.log(noSpaceStr);
return noSpaceStr;
}
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.109 Safari/537.36 OPR/84.0.4316.50
Challenge: Title Case a Sentence
Link to the challenge:
First, congrats on getting a working solution without using some very convenient array methods. Yes, it was more work, but I bet you have a good understanding of how this algorithm works 
But ya, in the “real world” you would definitely use array methods like join
to make this much more readable. For example, you can replace everything after the first for
loop with one return
statement using join
. And using join
is not a requirement here. You can use replace
with a proper regexp to really shorten the code down to almost nothing.
Now that you have a working solution I don’t see any harm in taking a look at the alternative solutions provided in the hints to get an idea of other ways to solve this challenge. Or, before you do that, try to challenge yourself by rewriting this using a functional approach and only a single return
statement.
1 Like
Note, it’s a bit easier if you put your question inside of your post instead of in the title
We have this handy spot for you to put the question.