Title Case a sentence -need help

Title Case a sentence -need help
0.0 0

#1

Hello fellow campers! I posted this same question under the guide forum for this particular exercise, but I’m putting it here as well in hopes to get some guidance.


I’ve had such hard time solving this exercise. I finally did it after looking at this page (guide for the title a case exercise), but it feels I have copied the answer even with minor tweaks.
I’ve copied 2 of my codes before I reviewed this site (I tried a few). 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

Hi veronica,

You have most of it right. Few corrections:

function titleCase(str) {
  var splitStr = str.toLowerCase().split(" ");
  var capitalizeFirstWord = splitStr.map(function (word) {
    return word.replace(word.charAt(0), word.charAt(0).toUpperCase());
  });
  return capitalizeFirstWord.join(" ");
}
                              
titleCase("i’m a little tea pot");
  1. You need to map over each word in splitStr. Read about map here
  2. Use charAt instead of chartAt

#3

thanks a lot! Using map makes sense, but can you use a for loop as well?


#4

Yes!! You can try the for..of syntax.

function titleCase(str) {
  var splitStr = str.toLowerCase().split(" ");
  var capitalizeFirstWord = [];
  for(let word of splitStr) {
     capitalizeFirstWord.push(word.replace(word.charAt(0), word.charAt(0).toUpperCase()));
  });
  return capitalizeFirstWord.join(" ");
}
                          
titleCase("i’m a little tea pot");