In the string, each word is separated by a space, and the number of spaces is not limited
function capitalize(sting) {
var words = string.split(" ");
for(var i = 0; i < words.length; i ) {
words [i] = words[i].charAt(0).toUpperCase() words[i].slice(1);
}
return words.join(" ");
}
var string = "ajax cookie event object";
capitalize(string); // "Ajax Cookie Event Object"
Pay attention to the key sentence in the code
words[i] = words[i].charAt(0).toUpperCase() words[i ].slice(1);
words[i].charAt(0).toUpperCase() just gets the first letter of the string and converts it to uppercase letters. It does not change the original string , so it needs to be concatenated with other characters in the original string and assign the new value to the original string