Title Case a Sentence. Hi I'd like some on why my code is not passing I'm very confused

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

Challenge: Title Case a Sentence

Link to the challenge:

How many spaces should be at the end of the sentence?

1 Like

Jeremy, hello there!)

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.

Hope that helps) Cheers

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.

I’m aware of what the problem is. I think that you have mistaken me for the OP…

1 Like

Oh thanks so much, I was so confused, that makes so much sense now. I really appreciate the help.

oooop thanks, that’s all you had to say hahaha, i appreciate it.

1 Like

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