What is the advantage of a for loop over map() in the 'Title Case a Sentence' algorithm and Other Questions?

What is the advantage of a for loop over map() in the 'Title Case a Sentence' algorithm and Other Questions?
0

#1

Link to guide
I don’t understand why the intermediate solution is better than the basic solutions. Mainly:

  • why is it better to use a for loop rather than map()? What are the advantages?
  • is there an advantage to using charAt() rather than accessing the character via string[index]?

Also, below is my solution for this challenge that I came up with before looking at the guide. Any critiques would be helpful. Thank you!

function titleCase(str) {
  var words = str.toLowerCase().split(' ');
  words = words.map(function (word) {
    return word[0].toUpperCase() + word.slice(1,word.length);
  });
  return words.join(' ');
}

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

#2

In this particular comparison of the Basic and Intermediate solutions, the Basic solution executes 30% faster (based on my limited testing) than the Intermediate. If you define better as faster, then the Intermediate solutions is not better than the Basic solution. I believe the Intermediate solution is considered more advanced than the Basic, because it creates a prototype function to use.

There really is no comparison, string[index] is more than 1000 times faster than charAt, string[index] only takes O(1) to access, where using a function like charAt creates additional overhead and slows down the overall process time.


#3

Thank you!

Do you know why fCC would choose to use charAt() instead of string[index]?


#4

I have no idea. I believe they are working through these old guides and trying to update them with better solutions.