Algorithm Challenge- Feedback needed

Kindly help review my solutions to the algorithm challenges I’ve attempted so far.
First one I’ll be posting is ‘Title Case A Sentence’ because I was working on that when I made the decision.

function titleCase(str) {
 var words = str.toLowerCase().split(' ');
  words.forEach(function(w, index, arr){
    firstChar = w.charAt(0);
    edit = w.charAt().toUpperCase();
    arr[index] = w.replace(firstChar, edit);
    
  });
  return words.join(' ');
}

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

Thanks!

Looks good. You could also do this with map and a little function chaining. This is a really powerful pattern that I am personally trying to use more with map/reduce/filter etc. Something like:

function titleCase(str) {
  return str.split(' ').map(function(word){
    return word[0].toUpperCase() + word.slice(1);
  }).join(' ');
}

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

I like this functional approach as it usually saves you a bit of code

4 Likes

That’s really cool man. Would take a while to practice.
Thanks!