Basic Algorithm Scripting - Title Case a Sentence

Tell us what’s happening:

Hello, I am currently trying to set the first letter to uppercase, but I’m starting to get a bit lost, could someone help me out?

Your code so far

function titleCase(str) {
  let strArr = str.split(" ")
  let newStr = ""
  for(let i = 0; i < strArr.length; i ++) {
    newStr += strArr[i][0].toUpperCase()
  }
  return str;
}

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36

Challenge Information:

Basic Algorithm Scripting - Title Case a Sentence

Try adding some console.log calls, to see in console what is happening at each step.

I’m getting IALTP :sweat_smile: I guess it’s just captilizing the first words and returning them

So the question now is - how to include the rest of the word as necessary?

I have no idea, maybe replace the lower case one with upper case with .replace?

Hmm, would it work if word has first letter repeating further?

How two strings can be joined together?

concatinating the two strings together +=

Okay, how would look the updated code then?

Never mind I did puesdocode and figured it out with a bit of searching.

function titleCase(str) {
  // First I split the string by white space making it like ["I'm", "a", "little", "tea", "pot"]
  // make first letter uppercase
  // make a local variable that contains the string, then make a global variable for the new string that I want to return

  let strArr = str.split(" ");
  let finalStr = []
  for(let i = 0; i < strArr.length; i++) {
    const lowCase = strArr[i].toLowerCase()
    const newStr = lowCase.charAt(0).toUpperCase() + lowCase.slice(1);
    finalStr.push(newStr);
  }
  return finalStr.join(" ");
}

console.log(titleCase("I'm a little tea pot"))