Fall 2008COP2500 Lab 4

 

 

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 PROMPT DIALOG box and IF-THEN-ELSE blocks. The PROMPT DIALOG BOX accepts an input from the user and 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 "cop2500lab4.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 fare depending on the age of the user. The specific format you use to display the results is up to you, but it should be clear what the output means (in other words, don't just list rows of numbers without words or labels describing them

    Train Fare Calculator.
    Section Pseudocode Comments
    INPUT:

    var age = prompt('How old are you',' ')

    We ask the user his/her age
    COMPUTATION AND OUTPUT:

    IF (age > 55 ) THEN
         Display 'You pay the senior fare'
    ELSE
         Display 'You pay the regular adult fare'
    ENDIF

    Convince yourself these
    comparisons provide the
    correct result.

    Train Fare calculator Part II
    SectionPseudocodeComments
    INPUT:

    var age = prompt('How old are you',' ')

    We ask the user his/her age
    COMPUTATION AND OUTPUT:
    IF (age > 0 && age <=12 ) THEN
        Display 'You pay child`s fare!'
    ELSE
        IF (age > 12 && age < 60)
         Display 'You pay the regular adult fare'
       ELSE
           Display 'You pay the senior fare'
       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.



  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.