Title Case a text, but the challenges not accepting it works

Hello, I have trouble on converting each first letter to upper case. So far i think the code is good (i even compare how my code works with the hint from FCC), but it wont work and the challenges even not passed. Please help me, is there any problem with my code or it was a bug from FCC itself

------My code


function titleCase(str) {
  let newStr = str.split(' ');
  let text = "";
  for(let i = 0; i < newStr.length; i++){
    let word = newStr[i].slice(0, 1).toUpperCase();
    word += newStr[i].slice(1, newStr[i].length).toLowerCase() + " ";
    text += word;
  }
  return text;
}

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

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/title-case-a-sentence

You’re adding an extra space at the end of every sentence.

Think about just rebuilding each word in the loop and pushing them to a new array, then using join with a space.

Or you can use trim on the final string to remove all spaces at the start/end.

wow i never thought that would be the error and your suggestion is really helpful, thank you.

1 Like