Putting Spaces between strings

Tell us what’s happening:
I think my code is right, so I am not exactly sure where I am going wrong. I think what might be the problem is that I have an extra space in my finalString, and in which case, I am not sure how to put the spaces in appropriately…

Your code so far


function titleCase(str) {
let lower = str.toLowerCase();
let arr = lower.split(" ");
let finalString = "";
let replaced = "";
let upper = [];
for(let i = 0; i < arr.length; i++){
  replaced = arr[i].replace(/^\w{1}/gi,
    arr[i][0].toUpperCase())
  upper.push(replaced);
  finalString = finalString + upper[i] + " ";
} console.log(finalString);

}

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

Challenge: Title Case a Sentence

Link to the challenge:

You may want to check out .join()

3 Likes