It's funny because I can do regular JAVA all day, but this stuff gives me a headache. I am sure it is so simple, but can anyone at least give me a hint on how to turn this while loop into a do while loop?
<!DOCTYPE html>
<html>
<head>
<meta charset = "utf-8">
<title>Class Average Program: Sentinel-controlled Repetition</title>
<script>
var total;
var gradeCounter;
var grade;
var gradeValue;
var average;
total = 0;
gradeCounter = 0;
grade = window.prompt(
"Enter Integer Grade, -1 to Quit:", "0" );
gradeValue = parseInt( grade );
while ( gradeValue != -1 )
{
total = total + 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>" );
}
else
document.writeln( "<p>No grades were entered</p>" );
</script>
</head><body></body>
</html>
Our teacher hasn't really given us a good example of transforming them from one to the other. I'm sure it's just a case of swapping a few words around, but I am honestly not sure.
Question
brentmcgehee
It's funny because I can do regular JAVA all day, but this stuff gives me a headache. I am sure it is so simple, but can anyone at least give me a hint on how to turn this while loop into a do while loop?
Our teacher hasn't really given us a good example of transforming them from one to the other. I'm sure it's just a case of swapping a few words around, but I am honestly not sure.
Link to comment
Share on other sites
4 answers to this question
Recommended Posts