Title Case a Sentence - not sure why solution isn't valid

I wrote this code and the output seems to be right when I console.log it but it is not accepting it as an answer. Would anyone be able to explain why? :frowning:

   **Your code so far**

function titleCase(str) {

str = str.toLowerCase().split(" ")
 var empt = "";
 
 for(var i=0;i<str.length;i++){
   var cap = str[i][0].toUpperCase();
   empt += str[i].replace(str[i][0],cap)+" ";
 } 
 
 return empt;
 
}


console.log(titleCase("I'm a little tea pot"));



   **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36

Challenge: Title Case a Sentence

Link to the challenge:

Hi @cyyc1498 !

Welcome to the forum!

You have extra space at the ends of your string.

You need to get rid of it.

If you google, “how to get rid of space at ends of string javascript”
then it will tell you the string method you can add at the end here

1 Like

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