Sign in to follow this  
Followers 0
brentmcgehee

Javascript homework help

5 posts in this topic

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.

Share this post


Link to post
Share on other sites

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 );
1 person likes this

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.