Hey all. Was hoping for a gentle nudge in the right direction. I’m on the
Basic Algorithm Scripting: Title Case a Sentence challenge. I need to capitalise the first letter of every word. This is what I have so far…
function titleCase(str) {
let lowerCase = str.toLowerCase();
let sentence = lowerCase.charAt(0).toUpperCase() + lowerCase.slice(1);
for (let i = 0; i < sentence.length; i++) {
if (sentence[i] === " ") {
console.log(sentence.charAt[(i + 1)]);
}
}
return sentence;
}
titleCase("I'm a little tea pot");
First step in the function is to change the complete string to lowercase in case I have a string passed that is completely capitalised. The second step is to capitalise the first letter of the string and store the result in the variable sentence.
Then I was going to use a for loop to look for any white spaces in the string. If it finds a space, I would then capitalise the very next letter in the sting. At the moment I’m just trying to console.log that letter. But something isn’t working. I was hoping to just use the toUpperCase method on the selected letter and that would do the job. Any tips? Am I nearly there. Any help would be gratefully received.