0

How can I change the content of the paragraph tag from HTML by JavaScript without using document.write? Why is it not showing when I view it in a browser? please help me learn this. Thanks

var grades = [45, 32, 66];
var sum = 0;

if (grades.length>0){
    for (index=0 ; index<grades.length; index++)
    sum += grades[index];
    console.log(sum);
    document.getElementById('here').innerHTML = sum/grades.length;
}

else
    document.getElementById('here').innerHTML = "Empty Array";
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Array average with zero</title>
    <script src="js/arrays2.js"></script>
</head>
<body>
    <p id="here">See Average Here</p>
</body>
</html>
  • Put the script tag just before `

    ` Right now, the `p` element doesn't exist when the script executes.

    – James May 10 '22 at 17:22
  • Thank you very much! Can you please explain it a little further? How come the – rjnpgarcia May 10 '22 at 18:08
  • I think the responses to [this question](https://stackoverflow.com/questions/436411/where-should-i-put-script-tags-in-html-markup) can answer that – James May 10 '22 at 18:14
  • I appreciate this! thank you so much! – rjnpgarcia May 10 '22 at 18:24

0 Answers0