Javascript Dilemma

Discussion in 'Web Design & Coding' started by D Schrute, Apr 16, 2005.

  1. D Schrute

    D Schrute Assistant Sensei VIP Member

    Joined:
    Aug 31, 2004
    Messages:
    1,201
    Likes Received:
    19
    Location:
    VA & NC
    I am having trouble on one of the many assignments in my last project of the year. The original page can be found at http://www.thecamelnet.com/php/ch08-fig09.html

    I need to add a feature that will find the highest of the entered numbers and print it in the end. I know what I need to do but I can't seem to get it to work... I don't think I am placing it in the correct area.... :?

    Original:

    Code:
     
    <!-- ch08-fig09.html -->
    <?xml version = "1.0"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
       "[url="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd[/url]">
    <html xmlns = "[url="http://www.w3.org/1999/xhtml"]http://www.w3.org/1999/xhtml[/url]">
       <head>
    	  <title>Daniel .::. ch08-fig09.html</title>
    	  <script type = "text/javascript">
    		 <!--
    		 var gradeCounter,  // number of grades entered
    			 gradeValue,	// grade value
    			 total,		 // sum of grades
    			 average,	   // average of all grades
    			 grade;		 // grade typed by user
    
    		 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>
    	  <p>Click Refresh (or Reload) to run the script again</p>
       </body>
    </html>
    
    What I am doing is creating a variable 'maximum' and starting it out as equal to zero. I am then doing

    Code:
    		 if ( gradeValue > maximum ) {
    			maximum = 0 + gradeValue;  
    			document.writeln( 
    			   "<h1>Class maximum is " + maximum + "</h1>" )
    
    This either changes nothing or causes a fatal error in the script... any ideas?
     
    D Schrute, Apr 16, 2005
    #1
    1. Advertisements

  2. D Schrute

    Cuddlez I'm Ron Burgundy? VIP Member

    Joined:
    Dec 21, 2004
    Messages:
    166
    Likes Received:
    6
    Location:
    O.C. Californi-ay
    Ok let me get this straight...

    You are entering a bunch of seperate grades, then running through a loop that will compare each grade and store the largest one in a variable called maximum?

    I checked the website and your average function works fine for me...

    why not integrate the average and maximum functions together?

    something like this (mind you this is psuedo Java/C++ syntax):

    Code:
    if ( gradeCounter != 0 && gradevalue > maximum ) {
    		 average = total / gradeCounter;
    		 maximum = gradevalue //Do you really need to say 0+gradevalue?
    		 document.writeln(the rest of the stuff here);
    		 }//End if statement

    gah! re-looking at your code I hope you remembered to close the if statement, since you don't have the closing bracket ( } ) in your code snippet for maximum function.

    Anyway that's all I got right now hope it helped.
     
    Cuddlez, Apr 19, 2005
    #2
    D Schrute likes this.
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.