Problem with challenge: Title Case a Sentence

Problem with challenge: Title Case a Sentence
0

#1

Hi!

I get the same output as the requirements states but only test I pass is: “titleCase(“I’m a little tea pot”) should return a string.”, any feedback on what I am doing wrong?


function titleCase(str) {
  
  str = str.split(" ", str.length);

  var upperCase = "";
  
  
  for(var i = 0; i < str.length; i++){
     
    
    for(var j = 0; j < str.length; j++){
      upperCase += str[j].substring(0, 1).toUpperCase() + str[j].substring(1, j.length).toLowerCase() + " ";
    }
    
    return upperCase;
    
  }
  
}

titleCase("HERE IS MY HANDLE HERE IS MY SPOUT");

EDIT: Aha maybe because it leaves a space on the end of the string?


#2

Yes, that would be the problem.


#3

function titleCase(str) {
  
  str = str.split(" ", str.length);
  
  var upperCase = "";
  
  
  for(var i = 0; i < str.length; i++){
     
    
    for(var j = 0; j < str.length; j++){
      if(j == str.length - 1)
        upperCase += "";
      upperCase += str[j].substring(0, 1).toUpperCase() + str[j].substring(1, j.length).toLowerCase() + " ";
    }
    
    return upperCase;
    
  }
  
}

titleCase("HERE IS MY HANDLE HERE IS MY SPOUT");

Hmm still does not work…


#4

This part:

str[j].substring(1, j.length).toLowerCase() + " ";

is still adding a space on the end.

str = str.split(" ", str.length);

After your first line code above runs, str becomes an array of words. You only need the first loop which will convert the first character of the word which would be str[i][0] to upper case and the concatenate the lower case version of str[i].substring(1, str[i].length) to it. Since you already mutated str from a string to an array, instead of assigning the concatenation above to a new string variable as you do with your upperCase variable, why not just assign the concatenation to str[i] to replace the current word of the array with the correct version? Then after the loop, you can use the str = str.split(" ", str.length);function to assemble the str array back to an actual string.


#5

Is that return statement inside of a for block?


#6

Hmm can’t really figure it out, still have the same problem with the space on the end of the string.

function titleCase(str){


  str = str.toLowerCase().split(" ");
  var copyStr = str;
  var capitalizedLetter = "";


  // capitalize each letter
  for(var i = 0; i < str.length; i++){
    capitalizedLetter += copyStr[i].charAt(0).toUpperCase()+copyStr[i].substring(1, copyStr[i].length)+" ";
  }




  return capitalizedLetter;


}

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

#7

You are adding the space right here:
image


#8

Yes I know.

Had to look at how others had solved the solution and tweaked my code a bit. Also added comments which I think describes each line. It works now.

  function titleCase(str){


    var arrayOfWords = str.toLowerCase().split(" ");
    var word = "", capitalizedLetter = "";

    for(var i = 0; i < arrayOfWords.length; i++){
      // One word is saved each iteration to word
      word = arrayOfWords[i];
      /* The first letter of the word is converted to uppercase
      and saved to capitalizedLetter*/
      capitalizedLetter = word.charAt(0).toUpperCase();
      /* word is redefined and the second letter till the last letter is saved to word e.g.
      I'm becomes 'm*/
      word = word.substring(1, word.length);
      // Each capitalized letter is concatenated with the substring word
      arrayOfWords[i] = capitalizedLetter.concat(word);
    }
    // str is converted to a string from an array again and a space is added after each element of the array
    str = arrayOfWords.join(" ");


    return str;


  }

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

#9

FYI:

capitalizedLetter = word[0].toUpperCase(); // references 1st character of word and makes it upper case

accomplishes the same thing as:

capitalizedLetter = word.charAt(0).toUpperCase();

#10

@randelldawson Thanks. I thought I had factored mine down to the minimum, but forgot I could reference strings in that manner. I’m going to put it on my ‘Learned Today’ list in order to make sure I don’t forget again.