So I have managed to separate the first letter of each word and capitalise it. Am I on the right track? Have been at this for a good few hours now and i’m not sure if I am overcomplicating things. I am now trying to figure out how to replace the start of each word in the string with the capital letters I have generated. Please don’t tell me the answer, small hints are fine. If I am way off please tell me and i’ll start from scratch.
When posting code, select all of the code in your post and hit the code button. This will make the code easier to read.
When initializing a variable to an empty string, you don’t need the parens, as you used with theCaps
var theCaps = “”;
As for the solution, you are quite close to getting the correct output. You have the first letter capitalized, now you just need to figure out where the rest of each word is and concatenate it on to the first letter.
Well this is how I have solved the problem with of course my cousin’s help to build it:
function titleCase(str) {
str = str.split(" ");
for(var i = 0; i < str.length; i++){
str[i] = str[i].toLowerCase();
str[i] = str[i][0].toUpperCase() + str[i].substring(1,str[i].length);
}
return str.join(" ");
}
titleCase("I'm a little tea pot");
titleCase("sHoRt AnD sToUt");
titleCase("HERE IS MY HANDLE HERE IS MY SPOUT");
I hope you find it helpful. Let me know if you want any comment on it.