ASDV-WebDev/Semester 1/Assignments/JavaScript/MP13_CalebFontenot/public_html/Problem6.html

67 lines
2.5 KiB
HTML

<!DOCTYPE html>
<!--
Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
Click nbfs://nbhost/SystemFileSystem/Templates/ClientSide/html.html to edit this template
-->
<html>
<head>
<title>Problem 6</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<style>
table, th, td {
border: 1px solid;
}
</style>
<body>
<div id="tableDiv"></div>
</body>
<script>
// Get user input
var array = [];
var numOfStudents = 0, numTests = 0;
numOfStudents = prompt("Enter the number of students to enter into the table.")
numTests = prompt("Enter the number of tests administered: ");
for (let i = 0, j = numOfStudents; i < numOfStudents; ++i, --j) {
var nameInput = prompt("Enter a student name. (" + j + " left).");
array[i] = [nameInput, undefined];
let testArray = [];
for (let k = 0; k < numTests; ++k) {
let grade = prompt("Enter " + nameInput + "'s grade for test " + (k + 1) + ": ");
testArray.push(grade);
}
array[i] = [array[i][0], testArray];
}
console.log(array);
let tableString = "<table>" + "<tr>" + " <th>Student Name</th>";
for (let i = 0; i < numTests; ++i) {
tableString += "<th>";
tableString += "Test " + (i + 1);
tableString += "</th>";
}
tableString += " <th>Grade Average</th>" + " </tr>";
for (let i = 0; i < array.length; ++i) {
tableString += "<tr>" + "<td>" + array[i][0] + "</td>";
for (let j = 0; j < numTests; ++j) {
tableString += "<td>" + array[i][1][j] + "</td>";
}
tableString += "<td>" + getAverage(array, i, numTests) + "</td>";
tableString += "</tr>";
}
document.getElementById("tableDiv").innerHTML = tableString;
function getAverage(array, testSet, numTests) {
console.log(array)
let gradeAverage = 0;
for (let i = 0; i < numTests; ++i) { //gradeArray[testSet].length
gradeAverage += parseFloat(array[testSet][1][i]);
}
console.log(gradeAverage);
gradeAverage = gradeAverage / numTests;
console.log("Grade average: " + gradeAverage);
return gradeAverage;
}
</script>
</html>