Javascript Dilemma

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

  1. D Schrute

    D Schrute Assistant Sensei VIP Member

    Aug 31, 2004
    Likes Received:
    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

    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.... :?


    <!-- ch08-fig09.html -->
    <?xml version = "1.0"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    <html xmlns = "[url=""][/url]">
    	  <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;  
    			   "<h1>Class average is " + average + "</h1>" );
    			document.writeln( "<p>No grades were entered</p>" );
    		 // -->
    	  <p>Click Refresh (or Reload) to run the script again</p>
    What I am doing is creating a variable 'maximum' and starting it out as equal to zero. I am then doing

    		 if ( gradeValue > maximum ) {
    			maximum = 0 + gradeValue;  
    			   "<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. Advertisements

  2. D Schrute

    Cuddlez I'm Ron Burgundy? VIP Member

    Dec 21, 2004
    Likes Received:
    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):

    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
    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.