Basic Algorithm Scripting - Title Case a Sentence

Hi, I have done this program and it seems to be okay. Also it matches the output string but Idk why test is failing, can anyone help me?

function titleCase(str) {
  let answer="";
  let words = str.split(' ');
  for(let x in words){
    answer = answer+words[x].charAt(0).toUpperCase()+words[x].slice(1,words[x].length)+" ";
 return answer;

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

Challenge: Basic Algorithm Scripting - Title Case a Sentence

Link to the challenge:


add this logs before final return statement, that should give you a hint

1 Like

Thanks, exactly but where’s that extra character!? Lol

I'm A Little Tea Pot*
I'm a little tea pot

It was there thanks!

I would think that is an extra space somewhere
That’s not all the issues I think
You code does not seem to handle cases where str has capitalized letters which are not at the start of words

titleCase("sHoRt AnD sToUt")//SHoRt AnD SToUt 

Ah, I did lower the str at the start of the function using .toLowerCase() The space was actually because I hade an extra space that the line added. I removed it with slice()
Thanks for your time :slight_smile:

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