How to approach this task... help

Tell us what’s happening:
Describe your issue in detail here.

  **Your code so far**

function titleCase(str) {


return str;
}

titleCase("I'm a little tea pot");
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.106 Safari/537.36

Challenge: Title Case a Sentence

Link to the challenge:

There are few ways to solve the challenge. The way you should choose depends on your abilities. One way to approach it is regex. In fact, you could utilize regex with different approaches. You could use regex to locate every word and change its letter to an upper case. You could use the String.replace() method along a proper regex.
Another approach would be to turn the string into an array of “words”, then obviously, loop thru that array and work out each word to have upper case. Or you could loop thru each letter in the initial string and have letters which are start of a word to be upper case(need to figure out how to put that condition!).
Keep in mind strings are immutable, which means you cant directly change parts of them, you need to build a new string with the changed value(s), where array values can be manipulated easier.
Thats the thrill of coding, you can solve one problem many way, just pick a path and see what works best for you, what works best in general. Id say working with strings, regex is most the time the better solution, so its a good skill to have under your belt.

1 Like

Hi~

Hope this method will be helpful to you!

Let string = "lorem ipsum dolor";
//Split it by spaces and declare a variable
splitString = string.split(' ');
//And then 
string[0].toUpperCase();

Use this in your functionn
btw, I advise you to learn more string methods

1 Like

@arsheencoder78618888

your solution does not pass the test.

@Sylvant
i will use strings along with a proper regex.
Thanks dude !

arsheen did not post a solution, but pseudo code for you to modify into real, working JS code.

Instead of regex, he suggests splitting the string into an array of words, then capitalizing each word in the array, then joining them back up. It would work.

2 Likes

Thanks, next time I will not repeat it!

1 Like

Hi @MatchaCrisp, @arsheencoder78618888

What you are saying, i got that thing last night but forget to comment here.
I am in also for this method also.
Thanks all of u !!

Happy Coding !!! :grin:

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.