Homework 4 | CS 4, Summer 2007 | Due: July 27



Directions: Submit hardcopies (paper copies) of your solution to the written questions (W). For the HTML questions (H), submit a hardcopy of your HTML code. Update your cs4/index.html to point to each HTML/Javascript solution. Make sure to place your name on the first page of your fully stapled homework. The point values for each question are as specified.
  1. (H: 50) Build a web page that allows a user to enter four exam grades (A,B,C,D) and compute the weighted average of these four exams, AVG = (A+B+C+2*D)/5. Your web page should contain five text objects and one button. Four of the text objects are used to enter the exam grades, and the fifth text object will display the computed average. The button should say "calculate", which when clicked checks to make sure that four grades have been entered, computes the average, and displays the answer. If all four exam grades have not been entered display a warning message using window.alert. Your web page should look something like:
       exam 1:  [       ]          [CALCULATE]
       exam 2:  [       ]
       exam 3:  [       ]
       exam 4:  [       ]
       average: [       ]
       
    HINT: when extracting the value from text objects you will need to convert from a string to an integer so that you can compute the average, e.g., var exam1 = parseInt(window.document.myform.exam1.value);

  2. (H: 50) Build a web page that allows a user to enter five exam grades (E1,E2,E3,E4,E5) and compute the weighted average as AVG = (E1+E2+E3+E4+2*E5)/6. Your web page should contain two buttons and a text object:
       [ENTER GRADES]     [CALCULATE]      average: [       ]
       
    When the ENTER GRADES button is clicked, a user should repeatedly be prompted to enter the five exam grades using the window.prompt command. These exam grades should be stored in an array. Use a for loop to read in the five exam grades (that is, you should not have five window.prompt commands in your program). When the CALCULATE button is clicked, check to make sure that five numbers have been entered, compute the average (using a for loop), and display the answer. If all five exam grades have not been entered display a warning message using window.alert.

    HINT: when extracting the value from the array you will need to convert from a string to an integer so that you can compute the average, e.g., var exam1 = parseInt(grades[0]);