Basic Algorithm Scripting - Title Case a Sentence

Tell us what’s happening:
So I am really struggling with this I guess. I wanted to have it replace the first letter of each word with the uppercase of it but using toUpperCase() doesn’t really work as that only capitalizes the whole string. I was thinking to use .replace but again I was unable to figure out how to only capitalize the first letter. I was also wondering if this was similar to the password assignment in the regex unit but on second thought I wasn’t sure if that was applicable. Anyway I would really appreciate some advice, since I can’t really browse the forum without influencing my code, I’m sure there’s a tool I’m probably forgetting or overlooking.

Your code so far

function titleCase(str) {
  //turns the whole string to lower case so the only thing needed is to capitalize the first letter of each word
  str = str.toLowerCase(str);
  //split the string to an array of words only
  let strArr = str.split(" "); 
  console.log(strArr);
  //iterate through the array of words
  for (let firstLetter = 0; firstLetter < strArr.length; firstLetter++) {
    //This should set th efirst letter of each word to upper case
    strArr[firstLetter][0] = ; 
    
  }
  //this will return the reworked string
  console.log(strArr);
}

titleCase("I'm a little tea pot");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/111.0

Challenge: Basic Algorithm Scripting - Title Case a Sentence

Link to the challenge:

You can use the replace method with a regex that matches every first letter and then use the replacer function to return the uppercase version of that letter.

1 Like