Title Case a Sentence - Expresion got ' ; '

Hi all I’m getting error expected expression got ‘;’

Any pointers?

function titleCase(str) {
     var strArray = str.split(" ");
     var e;
     var lowerCaseArray = for(e in strArray) {strArray[e].toLowerCase};
     var i;
     for (i in lowerCaseArray){
         moveUpperCase += this.replace(charAt(0), this.toUpperCase);
     str = moveUpperCase.join(" ");
  return str;

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

The line above is the problem. What exactly are you expecting this line to do? If you want to iterate through strArray and assign each element the lower case version of each element, then get rid of the var lowerCaseArray = part

What you were trying to do is not how you would do that. Also, note that you will then need to replace the reference to lowerCaseArray to strArray through out your code.

Since you did not seem to post the entire function, I can not tell if you initialized moveUpperCase to a value. If not, you are going to get a reference error there also.

Inside the for loop, you appear to want moveUpperCase to be an array and are trying to add values to it. The += operator is not the correct move here. You should review the previous challenge below for how to add elements to an array.


Also, what do you think this references in your solution? I think you will be very surprised.

1 Like

Your indentation suggests that your code is incomplete. I think it’s missing a few lines:

function titleCase() {
  var moveUpperCase = [];

Also, you can’t do this:

(This isn’t CoffeeScript or Python.)

What you should be doing is using the map function.

That would look like this:

var lowerCaseArray = strArray.map(e => e.toLowerCase());

By the way (although off-topic) you shouldn’t be using for..in for looping through elements in an array. You should be using a for..of loop instead. Besides, this looks logically wrong:

The this value you’re referring to is not really the one that you want. Here, this refers to the global object window. You don’t want that. Instead, you want lowerCaseArray[i].

Also, this:

The way I think you’ve intended to use replace is slightly different from how it actually works. Unfortunately, in JavaScript, there is no built-in function that replaces the nth character of a String. You’ll have to define your own.

Again, one more thing:

Again, you’re referring to this, which is not what I think you want to do. If I were you, I’d replace that with el.charAt(0). And toUpperCase is a function, you’re missing two brackets after it (toUpperCase()).

Oh, and as @RandellDawson had noted and I had missed, you can’t use += for adding elements to arrays. Try using Array.prototype.push().

Hope all this helped! :wink: