I'm working on a homework assignment that requires me to create a function that capitalizes each word in a sentence passed into it. My initial idea was to split the sentence into an array, loop through each word, capitalize the first letter of each word, and then join the words back together into a new string. Here is the code I have so far:
function titleCase(string) {
var words = string.split(' ');
for (var i = 0; i < words.length; i++) {
const lettersUp = ((words[i])[0]).toUpperCase();
const result = words[i].replace((words[i])[0], lettersUp);
return result;
}
}
The issue I am facing now is that the code only returns the first word of the input sentence. I suspect there might be an error in my loop logic, but I can't seem to pinpoint where the problem lies. Any assistance or guidance would be much appreciated. Thank you.