Nodejs String Title Case titleCase(str)

Here you can find the source of titleCase(str)

Method Source Code

/* Basic Algorithm: Title Case a Sentence

Return the provided string with the first letter of each word capitalized. Make sure the rest of the word is in lower case.

For the purpose of this exercise, you should also capitalize connecting words like "the" and "of".

Remember to use Read-Search-Ask if you get stuck. Write your own code.

Here are some helpful links://from w  w  w.j  av  a2 s  .  co m

String.prototype.split()

Written by: Sean M Hamlet
https://www.freecodecamp.com/seanmhamlet
*/

function titleCase(str) {
  var arr = str.split(" ");
  var first;
  var ends = [];
  var temp;
  var arrFixed = [];
  
  for (i = 0; i < arr.length; i++){
    first = [];
    ends = [];
    for (j = 0; j < arr[i].length; j++){
      if (j === 0){
        // make first letter capital
        first = arr[i][j];
        first = first.toUpperCase();
      } else if (j > 0){
        // make rest of letters lowercase
        temp = arr[i][j];
        ends[j-1] = temp.toLowerCase(); 
      }
    }
    arrFixed[i] = first + ends.join("");
    
  }
  str = arrFixed.join(" ");
  return str;
}

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

Related

  1. title()
    String.prototype.title = function() {
        return this.replace(/\w\S*/g, function(txt) {
            return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
        });
    };
    
  2. title()
    String.prototype.title = function() {
      return this.charAt(0).toUpperCase() + this.slice(1);
    };
    module.exports = String;
    
  3. titleCase()
    String.prototype.titleCase = function () {  
      return this.replace(/\w\S*/g, (txt) => {
        return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
      });
    };
    
  4. titleCase()
    String.prototype.titleCase= function()
        return this.charAt(0).toUpperCase() + this.substr(1);
    
  5. titleCase(str)
    function titleCase(str) {
      var arr = str.split(" ");
      var combineArr = [];
      var resultArr = [];
      for(var i = 0; i<arr.length; i++){
        var upperArr = [];
        var lowerArr = [];  
        for(var j = 0; j < arr[i].length; j++){      
          if(j === 0){
    ...
    
  6. titleCase(str)
    function titleCase(str) {
      var splitted = str.toLowerCase().split(' ');
      for (var i = 0; i < splitted.length; i++) {
        var holder = splitted[i];    
        var bigLetter = holder.charAt(0).toUpperCase();
        holder = holder.slice(1, holder.length);
        splitted[i] = bigLetter.concat(holder);
      str = splitted.join(' ');
    ...
    
  7. titleCase(string)
    function titleCase(str) {
        var newTitle = str.split(' ');
        var updateTitle = [];
        for (var st in newTitle) {
            updateTitle[st] = newTitle[st]
                .toLowerCase()
                .replaceAt(0, newTitle[st].charAt(0).toUpperCase());
        return updateTitle.join(' ');
    ...
    
  8. title_case()
    String.prototype.title_case = function () {
      var words = this.split(' ');
      var word = '';
      if (words.length > 1) {
        for (var i=0; i < words.length; i++) {
          if (words[i] != 'of' && words[i] != 'the' || i === 0){
            word = word.concat( words[i].charAt(0)
            .toUpperCase() + words[i].slice(1) + ' ');
          else {
            word = word.concat( words[i] + ' ');
        return word.trim();
      else {
        return this.charAt(0).toUpperCase() + this.slice(1);
    };
    
  9. titlecase()
    String.prototype.titlecase = function () {
      const wordArray = this.split(' ')
      const restrictedWords = ['a', 'the', 'an', 'and']
      const newWordArray = wordArray.map(word => {
        const downcaseWord = word.toLowerCase()
        if(restrictedWords.includes(downcaseWord)) return downcaseWord
        return word.capitalize()
      })
      newWordArray[0] = newWordArray[0].capitalize()
    ...