Title Case a Sentence need help

Title Case a Sentence need help
0

#1

I’m struggling with this one. This is what i have so far.

function titleCase(str) {
  var newStr = str.split(" ");
  console.log(newStr);
  var Arr = [];
  for(let i =0; i < newStr.length; i++){

     Arr[i] = newStr[i].charAt(0).toUpperCase();     
    }return Arr.join(' ');
  }


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

Output:
I A L T P


#2

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate 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.

Note: Backticks are not single quotes.


#3

You’re actually closer than you think.

As you have probably figured out you have so far captured the first letter of each word and capitalized it. That’s half the task so half way there. Plus you’ve conquered reuniting array elements back into a single string. There’s just a middle step missing.

You cannot mutate a string in place so you’ll need to think of a way to get the remaining letters merged back into a string with their corresponding (now capitalized) first letter.

I don’t have a link to that challenge. Were there any string methods suggested or hinted at in the instructions?


#4

I cant seem to figure out how to attach the rest of the string. This is the solution i keep seeing, but i really don’t understand it.

// 
String.prototype.replaceAt = function(index, character) {
    return this.substr(0, index) + character + this.substr(index+character.length);
};

function titleCase(str) {
    var newTitle = str.split(' ');
    var updatedTitle = [];
    for (var st in newTitle) {
        updatedTitle[st] = newTitle[st].toLowerCase().replaceAt(0, newTitle[st].charAt(0).toUpperCase());
    }
    return updatedTitle.join(' ');
}

Link to challenge


#5

I figured it out finally!!!