Given a list of grades, write a javascript to calculate average, number of passe
ID: 3533291 • Letter: G
Question
Given a list of grades, write a javascript to calculate average, number of passed, an number of failed grades, pass grade is >= 70. list terminates with -1
this is what i wrote
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Grade Cal</title>
<script type = "text/javascript">
<!--
var gradeCounter,
gradeValue,
totalpass,
totalfail,
average,
grade;
total = 0; // clear total
gradeCounter = 0; // prepare to loop
grade = window.prompt(
"Enter Integer Grade, -1 to Quit:", "0" );
gradeValue = parseInt( grade );
while ( gradeValue != -1 ) {
if( gradeValue >=70)
totalpass = totalpass + gradeValue;
gradeCounter = gradeCounter + 1;
if(gradeValue <70)
totalfail = totalfail + gradeValue;
gradeCounter = gradeCounter + 1;
grade = window.prompt(
"Enter Integer Grade, -1 to Quit:", "0" );
gradeValue = parseInt( grade );
}
if ( gradeCounter != 0 ) {
average = total / gradeCounter;
document.writeln(
"<h1>Class average is " + average + "</h1>" );
document.writeln(
"<h1>Number of Passed is " + totalpass + "</h1>" );
document.writeln(
"<h1>Number of Fails is " + totalfail + "</h1>" );
}
else
document.writeln( "<p>No grades were entered</p>" );
</script>
</head>
<body>
</body>
</html>
Explanation / Answer
check this code
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Grade Cal</title>
<script type = "text/javascript">
<!--
var gradeCounter,
gradeValue,
totalpass,
totalfail,
average,
grade,total;
total = 0; // clear total
gradeCounter = 0; // prepare to loop
totalpass=0;
totalfail=0;
grade = window.prompt(
"Enter Integer Grade, -1 to Quit:", "0" );
gradeValue = parseInt( grade );
while ( gradeValue != -1 ) {
if( gradeValue >=70)
{
total = total + gradeValue;
gradeCounter = gradeCounter + 1;
totalpass++;
}
else if(gradeValue <70)
{
total = total + gradeValue;
gradeCounter = gradeCounter + 1;
totalfail++;
}
grade = window.prompt(
"Enter Integer Grade, -1 to Quit:", "0" );
gradeValue = parseInt( grade );
}
if ( gradeCounter != 0 ) {
average = total / gradeCounter;
document.writeln(
"<h1>Class average is " + average + "</h1>" );
document.writeln(
"<h1>Number of Passed is " + totalpass + "</h1>" );
document.writeln(
"<h1>Number of Fails is " + totalfail + "</h1>" );
}
else
document.writeln( "<p>No grades were entered</p>" );
</script>
</head>
<body>
</body>
</html>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.