0% found this document useful (0 votes)
3 views1 page

5 05

The document presents a JavaScript program that calculates and displays the average, minimum, or maximum grade from a predefined list of grades. It provides a menu for the user to choose which calculation to perform. The program includes error handling for invalid user input.

Uploaded by

davisyang0427
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views1 page

5 05

The document presents a JavaScript program that calculates and displays the average, minimum, or maximum grade from a predefined list of grades. It provides a menu for the user to choose which calculation to perform. The program includes error handling for invalid user input.

Uploaded by

davisyang0427
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

//Davis Yang

//12/9/24
//5.05 Project Menu of Options

const grades = [65, 82, 74, 88, 36, 98, 100, 85, 50, 89];

console.log("Menu of Options");
console.log("================");
console.log("1. Average Grade");
console.log("2. Minimum Grade");
console.log("3. Maximum Grade");

const userChoice = parseInt(prompt("Which option would you like (1-3)? > "));

function calculate(gradesArray, choice) {


let result;

if (choice == 1) {
let sum = 0;
for (let i = 0; i < gradesArray.length; i++) {
sum += gradesArray[i];
}
result = sum / gradesArray.length;
console.log('The average grade is: '+' result');
} else if (choice == 2) {
result = gradesArray[0];
for (let i = 1; i < gradesArray.length; i++) {
if (gradesArray[i] < result) {
result = gradesArray[i];
}
}
console.log('The lowest grade is:'+' result');
} else if (choice == 3) {
result = gradesArray[0];
for (let i = 1; i < gradesArray.length; i++) {
if (gradesArray[i] > result) {
result = gradesArray[i];
}
}
console.log('The highest grade is: '+'result);
} else {
console.log("Invalid option. Please choose 1, 2, or 3.");
}
}

calculate(grades, userChoice);

You might also like