Capitalize First Letter of Each Word

I’ve managed to capitalize all of the letters, although I’m not being able to add all of the arrays back together again. And I wanted to ask you guys if there’s a way of doing this. Thanks in advance.

  **Your code so far**

function titleCase(str) {

let lowerStr = str.toLowerCase();
let splitString = lowerStr.split(" ");

for (let i = 0; i < splitString.length; i++) {
    let acessArr = splitString[i][0].toUpperCase();
    let replace = splitString[i].replace(splitString[i][0], acessArr);
    let join = [replace];
     console.log(join); 
// Output: [ 'I\'m' ]
[ 'A' ]
[ 'Little' ]
[ 'Tea' ]
[ 'Pot' ]
   
}


return str;
}

titleCase("I'm a little tea pot");
  **Your browser information:**

User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:97.0) Gecko/20100101 Firefox/97.0

Challenge: Title Case a Sentence

Link to the challenge:

This might be what you are looking for

your current code produces single element arrays, you will have to have an array declared outside of the for loop, then inside the for loop you push the capitalized string into that array.

1 Like

Thank you, it worked out. I’ve had to do some changes and convert the array into a string and remove the commas, but as you said I had to declare an array outside the for loop and push the array with capitalized letters outside the for loop and in the end I had to format the string a little bit.

Here’s the final code.

function titleCase(str) {

let lowerStr = str.toLowerCase();
let splitString = lowerStr.split(" ");
let storage = ;

for (let i = 0; i < splitString.length; i++) {
let acessArr = splitString[i][0].toUpperCase();
let replace = splitString[i].replace(splitString[i][0], acessArr);
let join = storage.push([replace]);

}

let convert = storage.toString();
let removeComma = convert.replace(/,/g, " ");
let finalString = removeComma;

return finalString;

}

titleCase(“I’m a little tea pot”);

1 Like