Title Case string help needed

Tell us what’s happening:
Why this code is not working? It is giving proper output but not accepting by freecodecamp.

Your code so far


function titleCase(str) {
let str1=str.toLowerCase().split(' ');
let tu =''
for(let i=0;i<str1.length;i++){
  tu += ' '+str1[i].replace(str1[i].charAt(0),str1[i].charAt(0).toUpperCase());
}

return tu;
}

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/83.0.4103.97 Safari/537.36.

Challenge: Title Case a Sentence

Link to the challenge:

what do you think happens at first iteration here?

spaces are characters too, if you add spaces in the wrong place you have the wrong string

But output is same as needed…but not accepting by free codecamp…

output is========I’m A Little Tea Pot
which is perfect

try writing console.log(JSON.stringify(titleCase("I'm a little tea pot")))

even if you don’t see it, the extra space is there

can you help me please??:sweat_smile:

I dont know much about JSON actully…Can you explain me this line

Your output is not "I'm A Little Tea Pot", it’s " I'm A Little Tea Pot". There’s a space at the front.

2 Likes

you don’t need to know about JSON, it’s just a way to show that extra space

you can also do console.log('>' + titleCase("I'm a little tea pot") + '<')

1 Like

Thank you…Funny mistake actually :joy:

Thank you for helping…:pray:

the problem is solved…:slightly_smiling_face: