Basic Algorithm Scripting - Title Case a Sentence

Hi! This is my code so far. I´ve passed all the test, except the one with the string “I’m a little tea pot”, My code returns “I’M A Little Tea Pot” instead of “I’m A Little Tea Pot” and I don´t know how to fix it. Thank you in advanced!

Your code so far

function titleCase(str) {
  let regex = /\w+\D/ig;
  let words = str.match(regex);
  let newStr = "";
  for (let word in words){
    newStr += words[word][0].toUpperCase() + words[word].slice(1).toLowerCase();
  }
  return newStr;
  }

console.log(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/107.0.0.0 Safari/537.36

Challenge: Basic Algorithm Scripting - Title Case a Sentence

Link to the challenge:

log your words to see if that makes it clearer:

function titleCase(str) {
  let regex = /\w+\D/ig;
  let words = str.match(regex);
  console.log(words)
...
}

Ohh okay, now I get way it does not work. The problem is that it separetes the " I’m " after the apostrophe instead of before (‘I'’, 'm ').
I’m not sure how to fix it, since if I change the regex for /\D\w+/ig it completely ignores the “I”

lol, i’m rubbish at regex.
i got this working:

  let regex = /[a-z]+\'*[a-z]*\w*/ig;

but i might of had to change a detail or two in the code elsewhere i cant remember and gotta go now

1 Like

Thank you very much! I could finally make it work

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.