• 0

Javascript homework help


Question

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.

Link to comment
Share on other sites

4 answers to this question

Recommended Posts

  • 0

You're right?it is just a case of swapping a couple of words around, but some JavaScript beginners may forget about the semicolon at the end of the 'while' part of the block  :)

 

I was going to attempt to just give a hint, but I don't see a problem with just informing you of how a do while loop looks.

 

Try the following:

do
{

total = total + gradeValue;


gradeCounter = gradeCounter + 1;


grade = window.prompt( 
"Enter Integer Grade, -1 to Quit:", "0" );


gradeValue = parseInt( grade );

} while ( gradeValue != -1 );
  • Like 1
Link to comment
Share on other sites

  • 0

gah im with you ... i hate JS...I can program PHP, sql, java etc like a ninja... but front end scripts kill me

Same here php, java, C all make more sense :P

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.