Merge branch 'main' of https://git.levimclean.me/Levi/IWD2-01
This commit is contained in:
BIN
INFO-1272 (JS 1)/Labs/Lab 5/Lab 5.pdf
Normal file
BIN
INFO-1272 (JS 1)/Labs/Lab 5/Lab 5.pdf
Normal file
Binary file not shown.
93
INFO-1272 (JS 1)/Labs/Lab 5/Levi_Lab5.html
Normal file
93
INFO-1272 (JS 1)/Labs/Lab 5/Levi_Lab5.html
Normal file
@@ -0,0 +1,93 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<title>Lab 5</title>
|
||||
<meta charset="utf-8" lang="en">
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h1>Lab 5</h1>
|
||||
<script>
|
||||
// Get number of students in class
|
||||
var numStudents = parseInt(window.prompt("How many students are in the class?"));
|
||||
var studentNames = [];
|
||||
var studentGrades = [];
|
||||
|
||||
// Run grade analytics once so theres something to display
|
||||
gradeAnalyzer();
|
||||
calculateAverage(studentGrades);
|
||||
findMinMax(studentGrades);
|
||||
|
||||
// Ask user if they want to review the results
|
||||
do {
|
||||
displayResults();
|
||||
var viewAgain = window.confirm("Would you like to view the results again?");
|
||||
} while (viewAgain);
|
||||
|
||||
// Thank user and end loop
|
||||
alert("Thank you for using the Grade Analyzer!");
|
||||
|
||||
// Function Definitions from here on
|
||||
|
||||
// Gets names, grades and inserts into respective arrays
|
||||
function gradeAnalyzer() {
|
||||
for (var i = 0; i <= numStudents - 1; i++) {
|
||||
var studentName = window.prompt("Enter students name:");
|
||||
var studentGrade = parseFloat(window.prompt("Enter student grade:"));
|
||||
if (studentGrade > 0 && studentGrade <= 100) {
|
||||
studentNames.push(studentName);
|
||||
studentGrades.push(studentGrade);
|
||||
} else {
|
||||
alert("Invalid grade entered, skipping entry");
|
||||
}
|
||||
console.log("Student " + (i + 1) + ": " + studentName + " - Grade: " + studentGrade);
|
||||
}
|
||||
}
|
||||
|
||||
// Calculates the average of a given array
|
||||
function calculateAverage(arr) {
|
||||
var totalGrades = 0;
|
||||
for (var i = 0; i < arr.length; i++) {
|
||||
totalGrades += arr[i];
|
||||
}
|
||||
var average = totalGrades / arr.length;
|
||||
console.log("Class average: " + average.toFixed(2));
|
||||
}
|
||||
|
||||
// Finds the minimum and maximum value of an array by looping through it
|
||||
function findMinMax(arr) {
|
||||
if (arr.length === 0) {
|
||||
return
|
||||
}
|
||||
|
||||
var min = arr[0];
|
||||
var max = arr[0];
|
||||
|
||||
for (var i = 1; i < arr.length; i++) {
|
||||
var grade = arr[i];
|
||||
if (grade < min) {
|
||||
min = grade;
|
||||
}
|
||||
if (grade > max) {
|
||||
max = grade;
|
||||
}
|
||||
}
|
||||
|
||||
console.log("Min: " + min + " Max: " + max);
|
||||
}
|
||||
|
||||
// Helper function to display results cleanly
|
||||
function displayResults() {
|
||||
console.clear();
|
||||
console.log("Student List:");
|
||||
for (var i = 0; i < studentNames.length; i++) {
|
||||
console.log("Student " + i + ": " + studentNames[i] + " Grade: " + studentGrades[i]);
|
||||
}
|
||||
calculateAverage(studentGrades);
|
||||
findMinMax(studentGrades);
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
BIN
INFO-1272 (JS 1)/Labs/Lab 5/Levi_Lab5.zip
Normal file
BIN
INFO-1272 (JS 1)/Labs/Lab 5/Levi_Lab5.zip
Normal file
Binary file not shown.
Reference in New Issue
Block a user