Help With title case Challenge

Help With title case Challenge


function titleCase(str) {
  var arr=str.toLowerCase().split(" ");
  for(var i = 0; i<arr.length; i++){
    for(var j =0; j<arr[i].length; j++){
      arr[i][0] = arr[i][0].toUpperCase();
  return arr;

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

Can you help explain why this code doesn’t work and how i can correct it.


I’ve edited your post for readability. When you enter a code block into the forum, remember to precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.


You have two problems:
#1) You need to return a string and not array. You can use the join function in or before the return statement.

#2) The following line does not do what you think it does:

arr[i][0] = arr[i][0].toUpperCase();

Since arr[i] is a string (a word in this case), you can not change a single letter in the string with the [0] reference. JavaScript will silently fail without an error message. The reason why you can not do augment the string in this way was explained in a previous challenge (see below).

To fix this issue, you need to replace the entire string with a new string which has the first letter capitalized and the rest of the string in lowercase. I will let you figure that part out on your own.