Jump to content



Photo

Javascript homework help


  • Please log in to reply
4 replies to this topic

#1 brentmcgehee

brentmcgehee

    Neowinian

  • Joined: 08-December 10

Posted 10 October 2013 - 00:01

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.




#2 vetCalum

Calum

    Neowinian Senior

  • Joined: 10-January 07

Posted 10 October 2013 - 00:13

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 );


#3 OP brentmcgehee

brentmcgehee

    Neowinian

  • Joined: 08-December 10

Posted 10 October 2013 - 00:25

Thank you much!



#4 Original Poster

Original Poster

    C++ n00b

  • Tech Issues Solved: 1
  • Joined: 15-July 08
  • Location: my room
  • OS: windows 7, backtrack 5, OSx 10.6

Posted 16 October 2013 - 01:23

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



#5 +Seahorsepip

Seahorsepip

    http://seapip.com

  • Tech Issues Solved: 17
  • Joined: 23-January 11
  • Location: Netherlands
  • OS: Windows 8.1 Pro
  • Phone: Nexus 5

Posted 16 October 2013 - 10:07

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





Click here to login or here to register to remove this ad, it's free!