Title Case a Sentence - My code works, but could it be more efficient/neater?

Hi,

Just finished the ‘Title Case a Sentence’ task, and I’ve looked through other results on the forum but nothing is at all similar to mine that I’ve seen. Have I been really inefficient about this? I’ve only used one for loop and one new variable, but should I be doing something differently? I kinda feel like a kid still counting on my fingers here haha. Like it gets the result, but there must be a more elegant way…

Thanks for any advice :slight_smile:

function titleCase(str) {
str = str.toLowerCase();
str= str.split(" “);
var result = “”;
for (i =0;i< str.length;i++){
result += str[i][0].toUpperCase();
result += str[i].slice(1);
result += " “;
}
result = result.replace(/\s*$/,””);
return result;
}

titleCase(“ShOrT aNd StOuT”);

1 Like

Yes, there’s a more elegant way.

Here
function titleCase(str) {
  return str.split(' ')
    .map(word => word[0].toUpperCase() + word.slice(1).toLowerCase())
    .join(' ');
}
4 Likes

The point is you can apply logic to the pass the test, personally I try not to worry if my solutions could be smaller, not at this stage at least. Maybe because i know they always could.

Looking at the above from kevcomedia… its great but theres no way i would have come up with that at the time.

Mines even longer!

Good point :slight_smile: I did actually stop and have that moment of realisation, that it was the first time id got to the point of reflection. So far it’s been ‘does it work? Ok done’. And slightly more recently ‘do I actually understand why it works? Kinda? Ok done!’ haha
But yeah, as long as it works, and could work for any input (rather than being designed just to pass those particular tests) I’m pretty happy at this point. Onwards and upwards!

Commenting out the code at each section when finished is a very good way to understand the jibberish i’ve just typed out I find :smiley:

I also go check the wiki to see exactly how the better soultions work… no cheating tho! you’re only allowed to do that if passed, i’ll be watching haha :eye:

I use this one here:


function titleCase(str) {
  return str.toLowerCase().replace(/(^|\s)(\w)/g, (match, p1, p2) => p1 + p2.toUpperCase());
}

1 Like

Yeah, I agree with MARKJ78… kevcomedia gave a great solution, but I wouldn’t have been able to (and didn’t) come up with something like that as I was working through these algorithms. My solution was pretty much the same as MARKJ78, I even wrote a blog post about my long ass solution here… LOL! Live and learn.

thx for the compliments :slight_smile: The best part is that you can pretty much use the same thing to get camelCase or any other similar thing. Like Pig Latin:

function translatePigLatin(str) {
  return str.replace(/(.*?)([oaeiuy].*)/, (match, p1, p2) => p2 + (p1 || 'w') + 'ay');
}
2 Likes

Damn, I thought I did ok with mine, but a one liner?

function translatePigLatin(str) {
  var first = "";
  var rest = str.slice(0,1);
  var rgx = /[aeiou]/;
  for(var i=0; i < str.length; i++){
    if(rgx.test(str[0]) ===  true){
      return str.concat("way");
    }
    else if(rgx.test(str[i]) === true) {
      first = str.slice(0, i);
      return str.slice(first.length).concat(first + "ay");
    }
  }
}

translatePigLatin("glove");