freeCodeCamp Challenge Guide: Title Case a Sentence

Here’s my code:

function titleCase(str) {
var lowerCase = str.toLowerCase();
var newstr = lowerCase.split(’ ');
toUpperCase = newstr.map(function(x) {
return x.charAt(0).toUpperCase() + x.slice(1).toLowerCase();
});
var result = toUpperCase.join(" ");
return result;
}
titleCase(“I’m a LITtle tea pot”);

Hello here is that my fonction
function titleCase(str) {
return str.toLowerCase().split(’ ‘).map( function (word) {
return word[0].toUpperCase() + word.substr(1);
}).join(’ ');
}
but i wrote this code if i do a console.log() it print the right string but if i copied it in fcc editor it doesn’t work i don’t know why (THIS IS THE FIRST FUNCTION I WROTE)
function titleCase(str) {
str = str.toLowerCase();
var arr = str.split(" “);var st = “”;
for(var i = 0; i < arr.length; i++){
st += arr[i].replace(arr[i][0],arr[i][0].toUpperCase())+” ";
}
return str;
}
I started challenge from the top and here is my new script


function titleCase(str) {
  var arr = [];
  arr = str.split(' ');
  var retour = arr.map(function(element){
  	var tmp = [];
    tmp.push(element.charAt(0).toUpperCase());
  	for(var i = 1; i < element.length; i++){
    	tmp.push(element[i].toLowerCase());
    }
    return tmp.join('');
  });
  return retour.join(' ');
}

I don’t know why but I read the explanation and I can’t understand the solutions added. Although I solved the challenge, I think there is something wrong or advanced. here is my Solution:

function titleCase(str) {

var splitstr = str.toLowerCase().split(" “);
var result = [];
for (var x = 0 ; x < splitstr.length ; x++ ){
var word = splitstr[x].split(”");
var letter = word.shift();
var up = letter.toUpperCase();
word.unshift(up);
result.push(word.join(""));
}
return result.join(" ");
}
titleCase(“I’m and little tea pot”);

Try not to gouge your eyes out when you look at this solution:

function titleCase(str) {
  str = str.split(' ').map(function(val) {
    return val.split('');
  });
//   for (var i = 0; i < str.length; i++){
//     for (var j = 0; j < str[i].length; j++) {
//       return str[i][0].toUpperCase();
//     }
//   }
  return str.map(function(val) {
    for (var i = 0; i < val.length; i++) {
//       var testVar = val.slice(1, val.length).join('').toLowerCase();
//       return testVar;
      return val[0].toUpperCase() + val.slice(1, val.length).join('').toLowerCase();
    }
    }).join(' ');
}
titleCase("sHoRt AnD sToUt");

Looking at the answers after I did it made me go “Wow. I really made this even more difficult.”

My solution, with for loop and slice:

function titleCase(str) {
  var lowCase = str.toLowerCase();
  var splitStr = lowCase.split(' '); 
  
  for (var i=0; i<splitStr.length; i++) {
    splitStr[i] = splitStr[i].slice(0,1).toUpperCase() + splitStr[i].slice(1);
  }
  
  return splitStr.join(" ");
}
1 Like

My solution with reduce and a lot of methods:

function titleCase(str) {

return str.toLowerCase().split(" “).reduce(function(acc, item){
return acc.concat([item.replace(item.charAt(0), item.charAt(0).toUpperCase())]);
}, []).join(” ");

}

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

function titleCase(str) {
  str = str.toLowerCase().split(" ");                           //Make str all lowercase and convert to array 
  for (var i = 0; i < str.length; i++){                         //Loop through array elements
    str[i] = str[i].charAt(0).toUpperCase() + str[i].slice(1); 
  }
  return str.join(" ");
}

str[i] = str[i].charAt(0) replaces the entire array element with it’s own first letter. Followed by .toUpperCase to capitalize the word.

Because the new value assigned to that position is only the first letter of the word, we + str[i].slice(1) to attach the rest of the word. .slice(x) works by returning a substring from position x until the last position.

2 Likes

My solution with no manual loop :slight_smile:

function titleCase(str) {
  var arr = [];
  var splitted = str.toLowerCase().split(/\s+/g);
  splitted.map(function(x) {
    return arr.push(x.charAt(0).toUpperCase() + x.substring(1));
  });
  return arr.join(' ');
}

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

My poor and bad solution :

function titleCase(str) {
  var lCase = str.toLowerCase();
  var array = lCase.split(" ");
  var i = 0;
  var i2 = 0;
  var word;
  var letter;
  var result;

  while (i < array.length) {
  word = array[i];
  letter = word.charAt(0).toUpperCase();
    i2 = 0;
    while (i2 <= word.length) {
      if (i2 == 0) {
        if (i == 0) {
          result = letter;
        }
        else {
          result = result + letter;
        }
        
      }
      else {
        result = result + word.charAt(i2);
      }
      i2++;
    }
    if (i == array.length -1) {
      result = result;
    }
    else {
      result = result + " ";
    }
    
  i++;
  }
  return result;
  
}

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

Honestly, there’s no “bad solution”. If it gets the job done, it’s a solution. :slight_smile:

4 Likes

Here’s mine:

function titleCase(str) {
  var newArr = [];
  var newStr = "";
  var lowerCaseArr = str.toLowerCase().split(" ");
  for(i=0;i<lowerCaseArr.length;i++){
	newArr.push(lowerCaseArr[i].replace(lowerCaseArr[i][0], lowerCaseArr[i][0].toUpperCase()));
	}
  newStr = newArr.join(" ");
  return newStr;
}

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

The solutions are WAAAAY different. Didn’t even learn about the prototypes yet, so I’m a bit nervous that we’re expected to know stuff we haven’t yet covered. But at least I got it to work on my own.

2 Likes

Hi, I didn’t understand the solution above, so i made my one, And it’s work fine for me:

function titleCase(str) {
    var newTitle = str.toLowerCase().split(' ');
    var updatedTitle = [];
    newTitle.forEach(function(item){
        updatedTitle.push(item.replace(item.charAt(0), item.charAt(0).toUpperCase()));
     });
     return updatedTitle.join(' ');
}
titleCase("HERE IS MY HANDLE HERE IS MY SPOUT");
1 Like

I wrote the following code, and it works in the console here at code camp (and at the javascript sandbox), but I’m still getting X’s. Here’s my code:

function titleCase(str) {
  str = str.toLowerCase();
  str = str.split(" ");
  var sent = "";  

  for(var i = 0; i < str.length; i++){
	var first = str[i][0].toUpperCase();
    var second = str[i].substring(1, str[i].length);
	var word = first + second;
	sent += word + " ";
  }
  
  return sent;
}

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

Steps:
1 and 2. Turn str into lower case and split into individual words at each space.
3. Create empty string variable to place my new sentence in.
4. Loop through the original sentence, grab first letter of each word and turn to upper case,
deduct first letter of each word, combine upper case letter with deducted version of each word.
5. Put each new word into the sent var (plus a space).
6. Return new sentence

Not sure why code camp registers the right answer, but doesn’t accept it as correct.

1 Like

I wanted to create a solution that didn’t use toUpperCase() or toLowerCase() methods :slight_smile: I ended up using ASCII characters instead. I have attached the image of my solution below :slight_smile:

3 Likes

yes, I agree with you on that point “Didn’t even learn about the prototypes yet”

1 Like

This is my solution. Not the best, but I tried to do it without looking at the solution first.
By the way, I was kind of surprised toUpperCase() method worked on chars, I thought it was exclusively for strings…?

function titleCase(str) {
  var arr = str.toLowerCase().split(' ');
  var newArr = [];
  for(i = 0; i < arr.length; i++) {
    newArr[i] = arr[i].charAt(0).toUpperCase() + arr[i].substring(1);
  }
  return newArr.join(' ');
}

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

Here is my solution :

function titleCase(str) {

str = str.toLowerCase().split(’ ');

var newArray = str.map(function(name){
return name.charAt(0).toUpperCase() + name.substr(1).toLowerCase();
});

return newArray.join(’ ');
}

titleCase(“sHoRt AnD sToUt”);

question: Why are you assigning a value to arrayStr[i]?
E.g. arrayStr[i] = arrayStr[i].replace(/[a-z]/, arrayStr[i].substring(0,1).toUpperCase());

I tried using a different variable name, but it wouldn’t work any other variable.
E.g. newStr = splitStr[i].replace(splitStr[i].charAt(0),
It’s a global variable; I had already defined it.

Thanks for any guidance!

Hi all,

I’ve had such hard time solving this exercise. I finally did it after looking at this page, but it feels I have copied the answer even with minor tweaks.
I’ve copied 2 of my codes (I tried a few) before I reviewed this site. They DO NOT work, but not sure why. What am I NOT seeing? Please Help!!!

ATTEMPT #1

function titleCase(str) {
var splitStr = str.toLowerCase().split(" “);
var capitalizeFirstWord = (function(){
splitStr.replace(splitStr.chartAt(0), splitStr.chartAt(0).toUpperCase());
});
return capitalizeFirstWord.join(” ");
}
titleCase(“i’m a little tea pot”);

ATTEMPT #2
function titleCase(str) {
var splitStr = str.toLowerCase().split(" “);
var capitalizeFirstWord = function(val){
return val.replace(val.chartAt(0), val.chartAt(0).toUpperCase());
};
return capitalizeFirstWord.join(” ");
}
titleCase(“I’m a little tea pot”);

IN ADVANCE THANKS SO MUCH FOR YOUR GUIDANCE!!!

2 Likes

My approach on this challenge:

function titleCase(str) {

var arr = [];

var upperCase;
var lowerCase = str.toLowerCase();

var words = lowerCase.split(’ ');//array of lowerCase words

for(var i = 0;i < words.length;i++) {
upperCase = words[i][0].toUpperCase() + words[i].substr(1);

arr.push(upperCase);

str = arr;

}

str = str.join(" ");

return str;
}

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