jj9667
jj9667

Reputation: 1

Having some trouble adding in code to give me the average in JavaScript

alert("Please provide the test scores for each student to calculate the average test score for the class");
var count = 1; //counter variable
var totalScore = 0; //to hold the calculated score amt
var finish = false; //boolean variable 
do {
  var scores = prompt("Enter student " + count + " or enter -1 to end the process.");
  if (scores.toUpperCase() != "-1") {
    totalScore = totalScore + parseInt(scores);
    count = count + 1; //count++   increment count by 1       
  } else { //user enter “End”
    finish = true;
  }
} while (!finish);
document.write("The total scores is " + totalScore);

for (count = 1; count <= 100; count++) { //average score
  total = total + count;
}
//Display the average of the numbers.
document.write("The average test score for the class is " + total);
<!DOCTYPE html>
<!--use Iteration (repetition)(looping) statements-->
<html>

<head>
  <title>do/while loop Statements</title>
</head>

<body>
  <h1>Running total example</h1>
  <h1>Sentinel value to control the numbers of student test scores to enter</h1>
  <h1>To calculate a total scores of all students</h1>
  <h1>and the average test score for the class</h1>
  <script language="JavaScript">
  </script>
</body>

</html>

So far this is what I have. I am trying to add in the average score. This is what it should look like with the average score:

enter image description here

Upvotes: 0

Views: 75

Answers (1)

Unmitigated
Unmitigated

Reputation: 89374

  1. count should start at 0.
  2. The average is simply totalScore / count.
alert("Please provide the test scores for each student to calculate the average test score for the class");
var count = 0; //counter variable
var totalScore = 0; //to hold the calculated score amt
var finish = false; //boolean variable 
do {
  var scores = prompt("Enter student " + count + " or enter -1 to end the process.");
  if (scores.toUpperCase() != "-1") {
    totalScore = totalScore + parseInt(scores);
    count = count + 1; //count++   increment count by 1       
  } else { //user enter “End”
    finish = true;
  }
} while (!finish);
document.write("The total scores is " + totalScore);
//Display the average of the numbers.
document.write("The average test score for the class is " + totalScore / count);

Upvotes: 3

Related Questions