freeCodeCamp:Title Case a Sentence

Hello, everyone.
I am doing this challenge:

When I finished my code, I got an error message. That is, TypeError: cannot read property ‘split’ of undefined.:roll_eyes::roll_eyes:
How can I solve this problem?

function titleCase(str) {
  // 请把你的代码写在这里
  var wordStr=str.split(" ");
  //for 為每個元素的第一個字母轉大寫
  for(var i=0;i<str.length;i++){
    var charStr=wordStr[i].split("");//將每個元素再拆一次,傳入一個arr
  str=wordStr.join(" ");
  return str;

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

Hi @a90100. You need to start debugging from the for loop. Your for loop is supposed to loop through wordStr, not str. The first line of code is fine. Hope this helps.

Oh,I didn’t notice it.Thank you so much.:smiley::smiley:

You are welcome. Have fun with the rest of the challenges!

1 Like