I have a string of text that I've split into an array on each comma. I then looped through the array and added each element to a string, one by one, but separated them using a line break.
var beg2 = document.twocities.begins.value;
var ans22 = "";
var words2 = beg2.split(",");
for(var i=0; i<words2.length; i++){
ans22 += words2[i] + "<br>";
}
document.getElementById("ans22").innerHTML = ans22;
Now I'm trying to capitalize the first letter of each line using this code but only the first letter of the entire string ends up getting capitalized as opposed to the first on each line.
var ans23 = "";
for (var i=0; i<words2.length; i++){
firstLetter = words[i].charAt(0);
firstLetterCap = words[i].charAt(0).toUpperCase();
words[i].replace(firstLetter,firstLetterCap);
ans23 += words2[i] + "<br>";
}
Any suggestions would be greatly appreciated.
replace()does not change a string in place, you have to save the return value from it