I'm having a strange issue where 'document.write' will print up into the 'For' loop, but will not print after the 'For' loop. In the function test(), 1st and 2nd 'document.write' will print, but the 3rd one does not. Please help.
<script>
const myForm=document.getElementById("myAvgForm");
const csvFile=document.getElementById("csvFile");
myForm.addEventListener("submit", function (e) {
e.preventDefault();
const f = csvFile.files[0];
const reader = new FileReader();
reader.onload = function (e) {
test(document.getElementById("symbol").value, e.target.result);
};
reader.readAsText(f);
});
function test(symbol, text)
{
var lines = text.split("\n");
symbol = symbol.toUpperCase();
document.write("Begining of file<br>"); // 1. prints
for (var i=0; i<lines.length; i++) {
var fields = lines[i].split(",");
if (fields[2].includes(symbol)) {
document.write(i + ". " + lines[i] + "<br>"); // 2. prints
}
}
document.write("End of file"); // 3. doesn't print
}
</script>