Files
ASDV-WebDev/Semester 1/Assignments/JavaScript/ch05/ch05/ex_5_7.html
2023-08-16 17:31:33 -05:00

67 lines
2.2 KiB
HTML
Executable File

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Example 5.7</title>
<script>
function getStats()
{
var score = 0; var sum = 0; var count = 1; var average = 0;
var high = 0; var low = 0;
score = parseInt(prompt("Enter a score or enter -999 when you're finished:"," "));
while (isNaN(score))
{
score = parseInt(prompt("Enter a valid score or enter -999 when you're finished:"," "));
}
low = score;
high = score;
while (score != -999)
{
sum = sum + score;
count++;
score = parseInt(prompt("Enter a score or enter -999 when you're finished:"," "));
if (score > high)
high = score;
if ((score < low) && (score != -999))
low = score;
}
average = Math.round(sum/(count - 1));
document.write("<p>The number of scores entered is: " + (count - 1) + ".</p>");
document.write("<p>The average of these scores is: " + average + ".</p>");
document.write("<p>The lowest score is: " + low + ".</p>");
document.write("<p>The highest score is: " + high + ".</p>");
}
function getStudent()
{
var question = " ";
var name = " ";
name = (prompt("What is this student's name?"," "));
var oddCount = 0; var evenCount = 0; var count = 0;
alert("At each prompt enter 'y' if the student got the question correct or 'n' for incorrect");
for (count = 1; count < 21; count++)
{
question = (prompt("Question " + count + ": ", " "));
if ((question == "n") && ((count % 2) == 0))
evenCount++;
if ((question == "n") && ((count % 2) != 0))
oddCount++;
}
document.write("<p>Results for " + name + ":</p>");
document.write("<p>Out of the 20 questions on this exam: </p>");
document.write("<p>The number of odd questions missed is: " + oddCount);
document.write("<p>The number of even questions missed is: " + evenCount);
}
</script>
</head>
<body>
<table align ="center" width ="70%"><tr><td colspan ="2">
<h1>&nbsp;</h2>
<h1>Exam 1</h1>
<h3>Get a summary of exam results</h3>
<p><input type="button" id="scores" value="Class results" onclick="getStats();" /></p>
<h3>Get an individual student's results</h3>
<p><input type="button" id="studentscores" value="Student results" onclick="getStudent();" /></p>
</td></tr>
</table></body>
</html>