Create A Webpage
Create A Webpa
Create a webpage and an external JavaScript file to calculate average grades entered by the user using prompt boxes. The program should prompt the user to enter grades one after the other and stop when a negative number is entered. The program must then display the total of the grades entered, the number of grades, and the average. Include comments in your code to explain your logic. Ensure the user is informed about the program's purpose in the HTML file. Test your program across different browsers. Submit your ZIP folder containing both files after correcting any errors and showing your code to your instructor for grading.
Paper For Above instruction
Average Grades Calculator
This program allows users to input multiple grades through prompt boxes. To end input, enter a negative number. Afterward, the program displays the total sum of the entered grades, the number of grades entered, and the calculated average.
// Initialize variables for total sum of grades and count of grades
let totalGrades = 0;
let gradeCount = 0;
let inputGrade;
// Function to prompt the user for grades and calculate average
function calculateAverageGrades() {
// Loop to repeatedly prompt user for grades
while (true) {
// Prompt user for input
inputGrade = prompt("Please enter a grade (enter a negative number to stop):");
// Check if user pressed cancel
if (inputGrade === null) {
alert("Input cancelled. Exiting program.");
break;
}
// Convert input string to a float number
let grade = parseFloat(inputGrade);
// Validate input
if (isNaN(grade)) {
alert("Invalid input. Please enter a numeric grade.");
continue;
}
// Check for sentinel value (negative number) to stop
if (grade
// Do not include negative number in total or count
break;
}
// Add grade to total
totalGrades += grade;
// Increment the number of grades entered
gradeCount++;
}
// After loop ends, compute and display results
if (gradeCount > 0) {
let average = totalGrades / gradeCount;
alert("Total of grades: " + totalGrades.toFixed(2) + "\n" +
"Number of grades: " + gradeCount + "\n" +
"Average grade: " + average.toFixed(2));
} else {
alert("No grades entered to calculate an average.");
}
}
// Call the function on page load
window.onload = calculateAverageGrades;