freeCodeCamp:Title Case a Sentence

Hello, everyone.
I am doing this challenge:

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

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?
Thanks.:sunglasses:

function titleCase(str) {
  // 请把你的代码写在这里
  var wordStr=str.split(" ");
  //for 為每個元素的第一個字母轉大寫
  for(var i=0;i<str.length;i++){
    var charStr=wordStr[i].split("");//將每個元素再拆一次,傳入一個arr
    charStr[0]=charStr[0].toUpperCase();
    wordStr[i]=charStr.join("");//將分解的字母合併成一個單字
  }
  str=wordStr.join(" ");
  return str;
  //每分解一次就給string不同名稱,怕搞混
}

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