Title Case a Sentence help please

Tell us what’s happening:
My code seems to be getting each character at the 0 index and capitalizing it but I can’t figure out how to replace only the first character in the string instead replacing the whole string.

Your code so far


function titleCase(str) {
  let lowerCaseStr=str.toLowerCase();
  let strArray=lowerCaseStr.split(" ");
for(let a=0; a<strArray.length; a++){
strArray[a].replace(strArray[a].charAt(),
function(match){
let firstChar=match.charAt();
let upperCaseChar=firstChar.toUpperCase();
console.log(upperCaseChar);
upperCaseChar.slice()
return strArray.splice(strArray[a][0],1,upperCaseChar)
}
)

}
console.log(strArray);
}

titleCase("sHoRt AnD sToUt");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/title-case-a-sentence

removed

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

If you want to compare your solution to others, use the Get a hint button on the challenge and there are alternative solutions you can compare yours to. Also, you can probably search older posts using the forum search feature or google the challenge name and find more there.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

Thank you for understanding.

let str = "programming";
let newStr = str[0].toUpperCase() + str.slice(1); // Programming

Edit 2: Nvm I figured it out by using push()

function titleCase(str) {
  let lowerCaseStr=str.toLowerCase();
  let strArray=lowerCaseStr.split(" ");
  let newArray=[];
for(let a=0; a<strArray.length; a++){
  strArray[a].replace(strArray[a].charAt(),
function(){
let replaced= strArray[a][0].toUpperCase() +strArray[a].slice(1);
return newArray.push(replaced);
}
)
}
return newArray.join(" ");

}
titleCase("sHoRt AnD sToUt");

Edit: Nvm I figured out why it was only returning one, I was cutting off the for loop early, I changed my code to this and get back “Short And Stout” but I still can’t pass the test :(. I think its because I still have a space at the end of "Short and Stout ".

function titleCase(str) {
  let lowerCaseStr=str.toLowerCase();
  let strArray=lowerCaseStr.split(" ");
  let newArray="";
for(let a=0; a<strArray.length; a++){
  strArray[a].replace(strArray[a].charAt(),
function(){
let replaced= strArray[a][0].toUpperCase() +strArray[a].slice(1);
return newArray=newArray+replaced+" ";
}
)
}
console.log(newArray);
return newArray;
}
titleCase("sHoRt AnD sToUt");

I got it to replace only the first letter but I can’t seem to get it to return all the components of the string. Right now it only returns “Short”. I think I’m also doing something wrong because when I concatenate the string, there is a space at the beginning. I’m not sure how to join everything. I tried the .join() method but I don’t think that works because everything in newArray is already a string?

function titleCase(str) {
  let lowerCaseStr=str.toLowerCase();
  let strArray=lowerCaseStr.split(" ");
  let newArray="";
for(let a=0; a<strArray.length; a++){
  strArray[a].replace(strArray[a].charAt(),
function(){
let replaced= strArray[a][0].toUpperCase() +strArray[a].slice(1);
return newArray=newArray+ " " +replaced;
}
)
console.log(newArray);
return newArray;
}
}
titleCase("sHoRt AnD sToUt");