Hi, I’m very stuck at the moment and don’t know what to change or do, I’d love some explanation and help to guide on what I’m doing wrong. My code seems to get the correct answer but will pass, thanks.
**My code so far**
function titleCase(str) {
let result = ''
let lowerCase = str.toLowerCase()
let eachWord = lowerCase.split(' ')
for (let i = 0; i < eachWord.length; i++){
result += eachWord[i][0].toUpperCase()
result += eachWord[i].slice(1)
result += ' '
}
return result
}
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/95.0.4638.54 Safari/537.36
In any case, when you think “I don’t know what’s going on”, you should visualize your code. A good website for this is Python Tutor. Let the name not mislead you, you can visualize JS, Java, C and Python there. With this website, you can see how your variables change, what the result is and where something unexpected happens.
I’ve checked your code and the problem is that you have a space at the end. You need to put in a logic: if this is the last word, then don’t add space. But maybe there’s a more elegant solution? See if there are better ways to do this task with ES6 or other standards.