freeCodeCamp Challenge Guide: Title Case a Sentence

Hi there, here is my solution to the problem. Capitalizing the first character of each element of string array ‘str’ then ‘lowercasing’ the other characters in the remainder of each element. Join them all up afterwards…voila!

function titleCase(str) {
str = str.split(’ ');
var strlen = str.length;
var strarr = [];
var strslice = [];

for (var i = 0; i < strlen; i++)
{
strarr[i] = str[i].charAt(0).toUpperCase() + str[i].slice(1).toLowerCase();
}
return strarr.join(" ");
}

titleCase(“I’m a little tea pot”);

Your result is ' Short And Stout'
There is a space on the begin, so your submit failed

3 Likes

Someone could explain me what (L) means in the Advanced solution? Thank you :slight_smile:

3 Likes

Can anyone tell me why this code does not complete the challenge? I can’t seem to find the problem. It looks like it returns the right answer.

var fullStr = “”;
function titleCase(str) {
str = str.toLowerCase().split(" “);
for (var i = 0; i < str.length; i++){
var capitalize = str[i].substring(0,1).toUpperCase();
var loseFirst = str[i].substr(1);
fullStr = fullStr + capitalize + loseFirst +” ";
}
return fullStr.substring(0, fullStr.length -1);
}

titleCase(“sHoRt AnD sToUt”);

1 Like

False alarm. I didn’t declare the variable fullStr inside the function.

Here’s my solution!

function titleCase(str) {
var words = str.toLowerCase().split(’ ‘);
var upper = [];
for (var i = 0; i < words.length; i++) {
upper.push(words[i].charAt(0).toUpperCase() + words[i].slice(1));
}
return upper.join(’ ');
}

titleCase(“I’m a little tea pot”);

2 Likes

Can anyone help me, I have no idea why this doesn’t pass all cases.

function titleCase(str) {
var arr = str.split(’ ‘);
var newStr = ‘’;
for (var i = 0; i < arr.length; i++){
var word = arr[i].split(’’);
for (var j = 0; j < word.length; j++){
if (arr[i][j]==arr[i][0]){
newStr += arr[i][0].toUpperCase();
} else{
newStr += arr[i][j].toLowerCase();
}
}
if (i + 1 != arr.length)
newStr += ’ ';
}
return newStr;
}
titleCase(“I’m a little tea pot”);

Here is what I was able to come up with for my solution using splice.

function titleCase(str) {
var lowerCase = str.toLowerCase();

var wordArray = lowerCase.split(" ");
var newWord = [];
var newLetter = [];
var finalWord = “”;
for (i = 0; i < wordArray.length; i++){
newLetter += wordArray[i][0].toUpperCase();
newWord += wordArray[i].replace(wordArray[i][0], newLetter[i]) + ’ ';

}

newWord = newWord.split(" “);
newWord.splice(-1,1);
console.log(newWord);
finalWord = newWord.join(” ");

return finalWord;
}

titleCase(“I’m a little tea pot”);

While my code for this challenge was a little dirty, I was able to finish it. However, although the output is correct, it does not move on to the next challenge. Anyone else have this problem?

Same here! I wrote the following code which seems to give all the desired outputs, yet I only get a checkmark for the first criteria but a red X for the other three.

function titleCase(str) {
  var arr = str.split(" ");
  var arr2 = [];
  for (i = 0; i < arr.length; i++) {
    arr2 += arr[i].charAt(0).toUpperCase() + arr[i].slice(1).toLowerCase() + " ";
    }
  return arr2;
}

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

EDIT: Of course, I realized my error right after I posted. I changed the final return line to:

return arr2.slice(0, -1)`

Is there someone who would be able to verify why this is happening? Moderators perhaps?

[spoiler]function titleCase(array) {
  var finalArray = '';
  var lowerC = array.toLowerCase().split(' ');
  for(var i = 0; i < lowerC.length; i++) {
     var newArray = lowerC[i].replace(/[a-zA-Z]/, function(s) {return s.toUpperCase(); });
     finalArray += newArray + " "; 
  }  
  return ('"' + finalArray.slice(0, -1) + '"');
}[/spoiler]

I have also uploaded my function. Used the same .slice(0, -1). Works on Chrome Developer Tools. Why not here? Curious…

var i = 0;
function titleCase(str) {
var little = str.toLowerCase();
var words = str.split(" ");

while (i<words.length){
words.replace( words[i].charAt(0).toUpperCase(),words[i].charAt(0));
i++;

}
var joined=words.join();
return joined;
}

titleCase(“I’m a little tea pot”);

Why is my code wrong?

The “Basic” Code Solution doesn’t look basic at all. Am I the only one who found it super confusing?

The Intermediate Code Solution was a bit easier to understand…

14 Likes

Here is what I came up with L

function titleCase(str) {
  var arr1 = str.toLowerCase().split(" ");
  var arr11 = [];
  for (a = 0; a < arr1.length; a++) {
    arr11.push(arr1[a][0].toUpperCase() + arr1[a].slice(1));
  }

  var arr2 = arr11.join(" "); 
  return arr2;
}

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

My solution seems to be valid, why isn’t it working?

var string=[];
function titleCase(str) 
{ 
  str=str.toLowerCase().split(' ');
  for(var a=0;a<=str.length-1;a++)
  {
    string.push(str[a][0].toUpperCase());
    for(var i=1;i<str[a].length;i++)
    {
      string.push(str[a][i]);
    }
    string.push(" ");
  }
  return string.join('');
}

For me this worked out pretty easy! :slight_smile:

function titleCase(str) {

var splitted = str.toLowerCase().split(" ");

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

splitted[i]=splitted[i].charAt(0).toUpperCase() + splitted [i].slice(1);

} return splitted.join(" ");

}

titleCase(“i’m a liTTle tea pot”);

1 Like
                                MY SOLUTION
titleCase(Str) {
    
     var oldArr = str.toLowerCase().split(' ');
     var newArr = [];
     
     for (var i in oldArr) {
         var newWord = oldArr[i][0].toUpperCase() + oldArr[i].slice(1);
         newArr.push(newWord);
        }
     return newArr.join(' ');
}

there’s no need for:

for (var i = 0; i < oldArr.length; i++) {
}

because:

for (var i in oldArr) {
}

will loop through the entire length of oldArr without the need of a loop counter.

3 Likes

Solution: The online compiler thing does not like it when I place global variables for some reason?

function titleCase(str) 
{
  var string=""; //<----buffer variable
  str=str.toLowerCase().split(' ');
  
  for(a=0;a<=str.length-1;a++)
    {
      string+=str[a][0].toUpperCase();
      for(var b=1;b<str[a].length;b++)
        {
          string+=str[a][b];
        }
      if(a==str.length-1) continue;//
      else string+=' ';            // Do not place a space on a final word
    }
  return string;
}

titleCase("tHe wIZARD oF oZ"); 
//"The Wizard Of Oz"
function titleCase(str) {
  return str.toLowerCase()
         .split(' ')
         .map(word => word[0].toUpperCase() + word.slice(1, word.length))
         .join(' ');
}

10 Likes