Are my solutions elegant enough?

Tell us what’s happening:
So I passed this challenge and noticed, that I seem to often use ways to approach the tasks in a way that is too complicated/not really elegant.
I mean, it seems to work so far, however I am just a bit unsure about wether my solutions are too complicated and if so, wether I can do something about that aside from practice and looking at other solutions.

Thanks for your help!

Your code so far


function titleCase(str) {
  str = str.toLowerCase();
  let words = str.split(" ");
  let result = "";

  for (let i = 0; i < words.length; i++) {

    let newEnding = words[i].replace(/^[a-z]/, "");
    
    result += words[i][0].toUpperCase() + newEnding + " ";
    
  }
  return result.replace(/\s$/, "")
  
}

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

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1 Safari/605.1.15.

Challenge: Title Case a Sentence

Link to the challenge:

Welcome, michael.

Your code has been blurred out to avoid spoiling a full working solution for other campers who may not yet want to see a complete solution. In the future, if you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

Thank you.

The first step is always to get solutions that work. Working and ugly is better than not working and pretty. From there you can revisit, refactor, and improve.

2 Likes

Thank you, sorry about that! I will make sure to do that in the future!

I’m glad to hear that :D. Thank you!