I am currently working on a JavaScript program that is designed to capitalize the first letter of each word in a string while converting every other character to lowercase.
function titleCase(str) {
str = str.toLowerCase();
var array = str.split(" ");
for(var i =0; i< array.length ; i++){
array[i][0] = array[i].charAt(0).toUpperCase();
}
var finalString = array.join(" ")
return finalString ;
}
console.log(titleCase("I'm a little tea pot"));
Issue arises when
array[i].charAt(0).toUpperCase();
does not correctly pass its value to array[i][0]
. As a result, the program returns the string with all lowercase letters, rather than correctly capitalizing the first letter of each word.