I am writing codes to convert initial letters of each word in a sentence to upper case and the rest to lower case. When I generated a new array arr and try to pass a number to it, there would be an error.
Cannot set property '0' of undefined.
I have no idea why this is happening.
function titleCase(str) {
var word = str.split(" ");
var arr = new Array();
for(var i = 0; i<word.length; i++){
arr[i][0] = word[i][0].toUpperCase();
if(word[i].length>1)
for(var j = 1; j<word[i].length; j++){
arr[i][j] = word[i][j].toLowerCase();
}
}
str = arr.join(' ');
return str;
}
titleCase("I'm a little tea pot");
Thank you so much for your help!