Title Case a Sentence (capitalizing after a space)

Title Case a Sentence (capitalizing after a space)
0

#1

So apparently I’m approaching this from a different perspective than most, but I still can’t figure out where I’m going wrong. Any help would be appreciated. I am attempting to use a for loop to find every space and then replace the next character with a capital. This is the code I have thus far, but when I run it, I only get “x.join is not a function”. Trying to return x alone just gets me a blank answer, so I’m not really sure where I’m going wrong.


function titleCase(str) {
  var array = str.toLowerCase().split("");
  var x;
  for (var i =0; i < array.length; i++) {
    if (array[i] == " ") {
    x = array[i].replace(array[i+1], array[i+1].toUpperCase());
    } 
  }
return x.join("");
}
 

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

I may just be completely and utterly off by a mile, but alot of my look ups to the solution end up referencing a lot of stuff I don’t understand and haven’t been exposed to yet. Any direction would be greatly appreciated.


#2

This will take the empty string (" ") and try to find the next character (array[i+1]) in that string. That won’t work, because the next character is not inside array[i]. Instead you can access the next character with array[i+1] like you did inside replace(), and assign a value to it:

array[i+1] = array[i+1].toUpperCase();

You can then return array.join(""). If you try this, you will find that it will not capitalize the first letter, so you will have to think of something to fix that.


#3

Thanks for pointing that out. I also now see that this won’t successfully capitalize the first letter because there’s no space… yep back to the drawing board I suppose.


#4

Not the prettiest I suppose, but it worked…

function titleCase(str) {
  var array = str.toLowerCase().split("");
 array.unshift(" ");
  for (var i =0; i < array.length; i++) {
    if (array[i] == " ") {
   array[i+1]=array[i+1].toUpperCase();
    } 
  }
 array.shift(" ");
return array.join('');
}
 

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

Thanks for the clarification.


#5

You can modify your if statement so that if the current character is a space (you have that already) OR the current index is that of the first character. What variable is currently tracking index?

variable i

What index represents the first?

0


#6

That’s what counts…

One little thing :slightly_smiling_face: Instead of (un)shifting, you can also just uppercase the first letter ouside the loop:

function titleCase(str) {
  var array = str.toLowerCase().split("");
  array[0] = array[0].toUpperCase();  // <---
  for (var i =0; i < array.length; i++) {
    if (array[i] == " ") {
      array[i+1]=array[i+1].toUpperCase();
    } 
  }

  return array.join('');
}

EDIT: Just noticed @randelldawson post…
EDIT 2: @randelldawson’s solution is better, because there might be a case where you pass an empty string to the function. array[0] would then be undefined and give an error.


#7

Ok I get this is theory, but have no idea how to syntactically write that so that it will work…


#8

And so in that case with the empty string, would the (un)shifting still work vs the outside loop or would they both give an error? Just asking for clarification.


#9

Apparently not. According to MDN:

Note: When the string is empty, split() returns an array containing one empty string, rather than an empty array. If the string and separator are both empty strings, an empty array is returned.

So the .split() would return a string instead of an array. And .unshift() is an array function and will also give a TypeError.