1st piece of code allows me to use an alert to compile a list of grades:
2nd piece of code allows me to calculate the mean of a list of numbers but only when I manipulate the list from within the code:
I cannot figure out how to make the new list (when I enter a new number in the prompt) to automatically update the mean of the grades. I would have thought it would have been as easy as cutting and pasting the two parts together but I couldn't get it to work.
Any thoughts wouild be appreciated!
Thanks.
Code:
<body onload="loadGrades()">
<button onclick="myFunction()">Add a grade!</button>
<p id="grades"></p>
<script>
var grades = [10,12,13];
function loadGrades(){
document.getElementById("grades").innerHTML =grades;
}
function myFunction() {
var grade = prompt("what is the next grade?");
grades[grades.length]=grade;
document.getElementById("grades").innerHTML = grades;
}
</script>
</body>
Code:
<script>
var Grades = [10,20];
var sum=0;
if(Grades.length>0){
for(index=0;index<Grades.length;index++){
sum+=Grades[index];
}
document.write(sum/Grades.length);
document.write(" is the average of the following grades" + Grades);
}
else
document.write("Emptyy");
</script>
Any thoughts wouild be appreciated!
Thanks.
Comment