Basic Algorithm Scripting - Title Case a Sentence

Hi everybody!!
I need some explanation about my below code for this challenge. I think I did meet all the criterias to pass this one; unfortuantely, I didn’t. I did try on console, and it’s correct sofar.

Really appreciate for your time and help in advance. Peace!!

My code so far

function titleCase(str) {
  var strLowerCase = str.toLowerCase();
  var strLowerCaseArr = strLowerCase.split(" ");
  var newStr = "";

  for (var i = 0; i < strLowerCaseArr.length; i++){
    var eachWord = strLowerCaseArr[i];
    var eachWordArr = eachWord.split("");
    eachWordArr.splice(0, 1, eachWordArr[0].toUpperCase());
    var newEachWord = eachWordArr.join("");
    newStr += " " + newEachWord;
  return newStr;

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

Challenge: Basic Algorithm Scripting - Title Case a Sentence

Link to the challenge:

HI @TungTran !

Welcome to the forum!

Not quite.

You have a small spacing issue which is why it is not passing.

You should add this console.log below the one you have so you can compare your answer with the correct one and see the issue I am talking about

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

Then in order to fix the issue, you should google how to remove space from beginning of string javascript

Google will return results for methods that will help you.

Once you fix that, then it will pass

Thanks so much @jwilkins.oboe !! I got this. Problem solved.
I should return newStr.trim()
Really appreciate :grin:

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