Title Case a Sentence-can a solution be created from the mess ive made

Title Case a Sentence-can a solution be created from the mess ive made


Tell us what’s happening:

so far, I am seeing solutions like this.

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(' ');

I came up with this
Your code so far

function titleCase(str) {
  var low = str.toLowerCase('');
  var splitting = low.split(' ');
  var upperI = splitting[0];
  var upperArrI = upperI[0].toUpperCase();// turns i in i'm into I
  var upperA = splitting[1];
  var upperArrA = upperA.toUpperCase();// adds upper case to the a after the word i'm
  var upperL = splitting[2];
  var upperArrL = upperL[0].toUpperCase();
  var upperT = splitting[3];
  var upperArrT = upperT[0].toUpperCase();
  var replaceFirst = str.replace(str[0],upperArrI).replace(str[4],upperArrA).replace(str[6],upperArrL);
  var eraseHalf = str.substr(13);// gets rid of str first 13 strings 
  var replaceSecond = eraseHalf.replace(eraseHalf[0],'T').replace(eraseHalf[4],'P');
  //reversing str, targetting the first two arrays and joining them..
  //the final variable of the following variables will be joined to replaceSecond
//   var reverseStr = splitting.reverse(''); 
//   var reverseSplitting_1 = reverseStr[0];
//   var reverseSplitting_2 = reverseStr[1];
//   var reverseSplittingJoin = reverseSplitting_1.concat(' ',reverseSplitting_2);
  //var reverseStr - reverseSplittingJoin are arbitrary
  var first_second = replaceFirst.replace('tea pot',replaceSecond); //replaces tea pot with Tea Pot
  return first_second;

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

can a solution be created from the mess ive made?
Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36.

Link to the challenge:


It looks like you have written a function that will only work for “i’m a little teapot”. The idea is to write a function that will convert any string to title case.


Hey, thanks for your hint!
I googled what title casing is exactly(since i was clueless as to what it was lol) then I googled how to do it
and found this solution.

function titleCase(str) {
return str.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});// each word is now title cased

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

saving this to my notes.



I was looking around to see how other people approach this problem and whether my idea would be sound.

I did something really similar. Check this out:

function titleCase(str) {
    let words = str.match(/\S*[^\s]/g);
    return words.map(w => w[0].toUpperCase()+w.toLowerCase().slice(1,w.length)).reduce((h, j) => h + ' ' + j);
console.log(titleCase("I'm a little tea pot"));

In retrospect, I should’ve condensed mine by remembering a couple of handy functions (e.g. substr() and replace()), as this would reduce the overall number of function calls for the sake of performance.

Here’s my revision (looks similar to yours — pretty cool how we can use different regular expressions and sequences of function calls to achieve the same result!)

function titleCase(str) {
    return str.replace(/\S*[^\s]/g, w => w[0].toUpperCase()+w.toLowerCase().substr(1));