Help, I solved a bonfire correctly but FCC won't let me continue

Hello guys, here is my problem: I finally solved the “Title Case a Sentence” bonfire, it returns me " I’m A Little Tea Pot" but I still can’t pass the challange, I guess maybe my code is a little weird and FCC doesn’t think that it is correct.
Here is my code :

  function titleCase(str) {
  var allLower = str.toLowerCase();
  var words = allLower.split(" ");
  var firstLetter;
  var restOfTheString;
  var final = "";
  for(var i = 0; i < words.length; i++) {

    firstLetter = words[i].split("").shift().toUpperCase();
    restOfTheString = words[i].slice(1);
    final += " " + firstLetter.concat(restOfTheString);
    
    }
  return final;
  
}

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

I know that probably it itsn’t the most common or fastest solution but I struggled to not search for the direct answer so I came up with this solution, what do you think of it?

Your function returns " I’m A Little Tea Pot", note the empty space at the beginning of the string. FCC is looking for “I’m A Little Tea Pot”, no empty space at the start of the string.

Try adding a check in the for loop if this is the first iteration, i.e. the first word, then you don’t want to prepend it with " ".

Another approach would be use the trim() js function on your final string which will remove any leading/ending empty spaces and return characters.

1 Like

My bad, I didin’t notice the space before the first word. Thank you for your help, I added .trim() after return final and solved it. :slight_smile: