Basic Algorithm Scripting - Title Case a Sentence

**My code is working on the terminal but when i run the tests, it fails. Can someone help **

My code below*

function titleCase(str) {
let str_Splits = str.split(" ");
let results = ""
for (let i = 0; i < str_Splits.length; i++){
  results+= str_Splits[i][0].toUpperCase() + str_Splits[i].slice(1).toLowerCase() + " "
}
return results
}

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

Let’s tweak this for readability

function titleCase(str) {
  const str_Splits = str.split(" ");
  let results = "";
  for (let i = 0; i < str_Splits.length; i++) {
    results += str_Splits[i][0].toUpperCase() + str_Splits[i].slice(1).toLowerCase() + " ";
  }
  return results;
}

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

Now, let’s do one little change to the console log

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

What stands out?

1 Like

I have added trim() to the code and it worked.

function titleCase(str) {
let str_Splits = str.split(" ");
let results = ""
for (let i = 0; i < str_Splits.length; i++){
  results+= str_Splits[i][0].toUpperCase() + str_Splits[i].slice(1).toLowerCase() + " "
}
return results.trim()
}

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

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