Title case a sentence - cannot pass

Tell us what’s happening:

Cannot understand why the function is wrong :wink: Assigment - Return the provided string with the first letter of each word capitalized. Make sure the rest of the word is in lower case. For the purpose of this exercise, you should also capitalize connecting words like “the” and “of”.

Your code so far
function titleCase(str) {
let newStr = “”;
const strArr = str.split(" ");
for(let i = 0; i < strArr.length; i++){
let subStr = strArr[i].toLowerCase();
let tStr = subStr.replace(subStr.charAt(0) ,subStr.charAt(0).toUpperCase() )
newStr+= tStr + " ";
}
console.log(newStr)
return newStr;
}

titleCase(“I’m a little tea pot”);


function titleCase(str) {
 let newStr = "";
 const strArr = str.split(" ");
for(let i = 0; i < strArr.length; i++){
  let subStr = strArr[i].toLowerCase();
  let tStr = subStr.replace(subStr.charAt(0) ,subStr.charAt(0).toUpperCase() )
  newStr+= tStr + " ";
  }
  console.log(newStr)
 return newStr;
}

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

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36.

Challenge: Title Case a Sentence

Link to the challenge:

your code is ok, the problem is that at the end of the sentence it returns one more space to solve this in a simple way is that the return add
newStr.trim ();

what trim () does is remove space at the beginning and end of a string

1 Like