COP2500 Lab 3

 

 

Deliverables: To complete this assignment you must --
1.) Write the HTML and JavaScript page described below and demonstrate to the lab instructor that it operates correctly.
2.) Complete the lab during the time provided or email your results to your lab instructors.
3.) Demonstrate your program(s) to your lab instructor.

Introduction: The goal of this assignment is to give you experience working with IF-THEN-ELSE blocks. IF-THEN-ELSE blocks let you choose what parts of your code execute based on input data.

Procedures:

  1. Use a text editor to complete your assignment.
  2. Using your text editor create a new document named "cop2500lab3.html". Do not close out the window until after the lab instructor has had time to verify your work. Make sure you save the file as a plain text document with extension ".html". If you are using WordPad, it may try to save your file in Rich Text Format. Change the type to "Plain Text Document", or your HTML file will be improperly rendered. Also Remember to delete your files before leaving the lab.
  3. Convert the following two algorithms into JavaScript code within your HTML document. Your final web page should display the algorithm title, the input and the output. The specific format you use to display the results is up to you, but it should be clear what the input and output means (in other words, don't just list rows of numbers without words or labels describing them). The first algorithm calculates the maximum of three numbers. The second algorithm computes a letter grade from a numeric grade.

    Finding the largest number.
    Section Pseudocode Comments
    INPUT:var x, y, z; We require three numbers
    OTHER VARIABLES:var max;
    INITIALIZATION:

    x = 34, y = 10,
    z = 42

    COMPUTATION: IF (x > y) THEN
    max = x;
    ELSE
    max = y;
    ENDIF

    IF (z > max) THEN
    max = z;
    ENDIF
    Convince yourself these
    comparisons provide the
    correct result.
    OUTPUT:display max using
    document.write() or some other
    output function
    Remember to format your results
    in a sensible way.

    LetterGrade(x): Computes a letter grade.
    SectionPseudocodeComments
    INPUT: var NumGrade;

    This is the numeric grade,
    0 - 100 percent.
    Set NumGrade to 76


    OTHER VARIABLES:var AlphGrade; This is where we will
    store the letter grade.
    INITIALIZATION: NumGrade is given.
    COMPUTATION:
     IF (NumGrade > 89) THEN 
       AlphGrade = "A"; 
     ELSE 
       IF (NumGrade > 79) THEN 
         AlphGrade = "B"; 
       ELSE 
         IF (NumGrade > 69) THEN
           AlphGrade = "C"; 
         ELSE 
           IF (NumGrade > 59) THEN 
             AlphGrade = "D"; 
           ELSE 
             AlphGrade = "F"; 
           ENDIF 
         ENDIF
       ENDIF         
     ENDIF 
    Note the use of the
    "nested" IF-THEN-ELSE
    structures. It may be
    easier to do one at
    a time, then view it.
    OUTPUT: display AlphGrade using
    document.write() or some other
    output function



  4. Open a browser and verify that the file displays properly. Keep this window open until the instructor has had time to verify your work. In order to obtain full credit for the lab you need to email your source code to your grader, and you need to show him that it is working in the lab.

    DON'T FORGET THIS

  5. Note that the algorithms shown here are not working Javascript code - they are only pseudocode meant to demonstrate the working of the algorithm. The syntax used by Javascript is somewhat different - a trivial example is the fact that the 'if' and 'else' keywords should be in lower case for Javascript to recognize it, or else it's just considered nonsense. More importantly, there are no "ENDIF" statements in Javascript (so don't ever put one into your code - it won't understand and you'll just see a blank screen). The keyword "THEN" is not part of Javascript syntax either, so don't put it in there either. Braces ("{" and "}") are used to serve the same purpose - delimiting the boundaries of the code that is executed based on the if statement. Think of statements in braces as a unit in some sense - they're what we call blocks or compound statements.
  6. These blocks are an important part of Javascript syntax, and they will recur in virtually every important new language feature we introduce in the upcoming labs, so get used to the way they work. Remember that the braces enclose the statements that you want to execute, but usually there's something just before the opening brace. In this particular assignment, opening braces will either be preceded by a line of the form 'if(condition)' or the single word 'else'. Also note that compound statements may contain other compound statements, so be sure to indent your code. That way the braces won't line up in one long incomprehensible column, and you'll easily be able to see which closing brace matches which opening brace.