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");
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 @camperextraordinaire had noted and I had missed, you can’t use +=
for adding elements to arrays. Try using Array.prototype.push()
.
Hope all this helped! 