Tell us what’s happening:
So I passed this challenge and noticed, that I seem to often use ways to approach the tasks in a way that is too complicated/not really elegant.
I mean, it seems to work so far, however I am just a bit unsure about wether my solutions are too complicated and if so, wether I can do something about that aside from practice and looking at other solutions.
Thanks for your help!
Your code so far
function titleCase(str) {
str = str.toLowerCase();
let words = str.split(" ");
let result = "";
for (let i = 0; i < words.length; i++) {
let newEnding = words[i].replace(/^[a-z]/, "");
result += words[i][0].toUpperCase() + newEnding + " ";
}
return result.replace(/\s$/, "")
}
titleCase("I'm a little tea pot");
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1 Safari/605.1.15
.
Challenge: Title Case a Sentence
Link to the challenge: