|
Hello, I'm new to this and trying to validate user input to do the following:
If the user enters something other than a 1 or 2, display an error message using the alert dialog box.
Prompt the user for a valid number.
For every value input, if the value entered is other than 1 or 2, keep looping until the user enters a correct value.
There should be a total of 10 valid inputs.
Here is my code so far:
<head>
<title>Analysis of Examination Results</title>
<script type = "text/javascript">
<!--
// initializing variables in declarations
var passes = 0; // number of passes
var failures = 0; // number of failures
var student = 1; // student counter
var result; // one exam result
// process 10 students; counter-controlled loop
while ( student <= 10 )
{
result = window.prompt( "Enter result (1=pass,2=fail)", "0" );
if ( result == "1" )
passes = passes + 1;
else
failures = failures + 1;
student = student + 1;
} // end while
// termination phase
document.writeln( "<h1>Examination Results</h1>" );
document.writeln(
"Passed: " + passes + "<br />Failed: " + failures );
if ( passes > 8 )
document.writeln( "<br />Raise Tuition" );
// -->
</script>
</head>
<body>
<p>Click Refresh (or Reload) to run the script again</p>
</body>
</html>
|
|
|
|
|
|
|
// |