I'm stuck on Title case a sentence

I'm stuck on Title case a sentence
0

#1

function titleCase(str) {

var first =str.split(" ");

var theCaps = ("");

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

if (first[i][0] !== first[i][0].toUpperCase()){

var second = first[i][0].toUpperCase();

theCaps = theCaps + first[i][0];

theCaps = theCaps.toUpperCase();

var capsSplit = theCaps.split("");

console.log(capsSplit);
console.log(first);

}

completed = first.join(" “);
capleted = capsSplit.join(” ");

console.log(capleted);
console.log(completed);

}

}

titleCase(“im here now”);

this is what I have so far. It returns

I H N
im here now

So I have managed to separate the first letter of each word and capitalise it. Am I on the right track? Have been at this for a good few hours now and i’m not sure if I am overcomplicating things. I am now trying to figure out how to replace the start of each word in the string with the capital letters I have generated. Please don’t tell me the answer, small hints are fine. If I am way off please tell me and i’ll start from scratch.


#2

I’ve no doubt my solution was far from the most efficient, but I used .charAt() and .slice() within my answer. Maybe check them out


#3

split the text to an array : split();

with every array value you can use toUpperCase() in the first char joining the rest of the string using substring().

join every modified value to a new string and use that as the result value.

:slight_smile:


#4

Hi there,

Here’s a couple of quick tips:

  • When posting code, select all of the code in your post and hit the code button. This will make the code easier to read.

  • When initializing a variable to an empty string, you don’t need the parens, as you used with theCaps
    var theCaps = “”;

As for the solution, you are quite close to getting the correct output. You have the first letter capitalized, now you just need to figure out where the rest of each word is and concatenate it on to the first letter.


#5

Well this is how I have solved the problem with of course my cousin’s help to build it:

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

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

titleCase("I'm a little tea pot");
titleCase("sHoRt AnD sToUt");
titleCase("HERE IS MY HANDLE HERE IS MY SPOUT");

I hope you find it helpful. Let me know if you want any comment on it.


#6

Here is how I did it after consuming several hours:

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

  for(var i=0;i<array.length;i++){
    array[i] = (array[i][0].toUpperCase() + array[i].slice(1, array[i].length));
  }
  return array.join(" ")
}

#7

heres my way
function titleCase(str) {
return str.split(" “).map( function(val) {
return val.charAt(0).toUpperCase() + val.slice(1).toLowerCase();
}).join(” ");
}

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

#8

Thank’s for your post, I finally understood why my code didn’t pass over.


#9

I really like your way. Nice!