Advertisement
RokiAdhytama

Average - Chapter 8

Jul 2nd, 2022
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. <html xmlns = "http://www.w3.org/1999/xhtml">
  2. <head>
  3. <title>Class Average Program</title>
  4.  
  5. <script type = "text/javascript">
  6. <!--
  7. var total, // sum of grades
  8. gradeCounter, // number of grades entered
  9. gradeValue, // grade value
  10. average, // average of all grades
  11. grade; // grade typed by user
  12.  
  13. // Initialization Phase
  14. total = 0; // clear total
  15. gradeCounter = 1; // prepare to loop
  16.  
  17. // Processing Phase
  18. while ( gradeCounter <= 3 ) { // loop 10 times
  19.  
  20. // prompt for input and read grade from user
  21. grade = window.prompt( "Enter integer grade:", "0" );
  22.  
  23. // convert grade from a String to an integer
  24. gradeValue = parseInt( grade );
  25.  
  26. // add gradeValue to total
  27. total = total + gradeValue;
  28.  
  29. // add 1 to gradeCounter
  30. gradeCounter = gradeCounter + 1;
  31. }
  32.  
  33. // Termination Phase
  34. average = total / 3; // calculate the average
  35.  
  36. // display average of exam grades
  37. document.writeln(
  38. "<h1>Class average is " + average + "</h1>" );
  39. // -->
  40. </script>
  41.  
  42. </head>
  43. <body>
  44. <p>Click Refresh (or Reload) to run the script again</p>
  45. </body>
  46. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement