i have a task for my homework where i have to write a function that will capitalize each word in a sentence that is written into that function. The idea i had was to convert each word into an array, make a loop targeting first letter of each item of that array, and then turning that array back into a string. The code i came up with is this
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 problem i have now is that it returns only the first word of an array. From troubleshooting i have been doing i have a feeling i messed up the loop but i just have no idea how. Any help would be greatly appreciated. Thanks.