| |
Deliverables:
To complete this assignment you must --
1.) Write the HTML and JavaScript page described below.
2.) Email your results to your lab instructor.
Introduction:
: The goal of this assignment is to give you further experience with if-else blocks as well as getting input from the user (the person running your code) with prompt and displaying output to the user similarly with alert in a dialog box (popup) instead of using document.write.
Prompt shows a dialog box to the user and gets a string (not a value).
var a, str;
str = prompt("Enter an integer");
To get a number from the user, use (a) parseInt or (b) parseFloat, which will get either an (a) integer or a (b) decimal.
a = parseInt(str);
You may optionally, instead, wrap the calls:
a = parseInt(prompt("Enter an integer"));
The browser first performs the prompt(...), getting a string from the user, then parses (looks for) an integer in that string and finally assigns it to a.
Alert shows a dialog box to display text and values in a popup.
alert("a = " + a);
You are required to use alert to display your output instead of document.write in this assignment (only).
Procedures:
- Use a text editor to complete your assignment.
- Using your text editor create a new document named "cop2500lab4.html".
Make sure you save the file as a plain text document with extension ".html".
-
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).
-
Note that the Instructions shown here are not working Javascript code, it is up to you to convert this description of the program into working code. 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. More importantly, there are no "ENDIF" or "THEN" statements in Javascript. The braces ("{" and "}") are used to serve the same purpose - delimiting the boundaries of a block of code that is executed based on the if, treated as one statement by the browser. Think of statements in braces as a
unit in some sense - they're what we call blocks or compound statements.
Train Fare Calculator.
| Section |
Instructions |
Comments |
| INPUT: | var age; |
We ask the user his/her age, input as an integer using parseInt(prompt(...)) |
| COMPUTATION AND OUTPUT: | IF (age > 55 ) THEN
Display 'You pay the senior fare' using alert
ELSE
Display 'You pay the regular adult fare' using alert
ENDIF
| /td>
Train Fare calculator Part II
| Section | Instructions | Comments |
| INPUT: | var age; |
We ask the user his/her age, input as an integer using parseInt(prompt(...)) |
| COMPUTATION AND OUTPUT: |
IF (age > 0 && age <=12 ) THEN
Display 'You pay child`s fare!' using alert
ELSE IF (age > 12 && age < 60)
Display 'You pay the regular adult fare' using alert
ELSE
Display 'You pay the senior fare' using alert
ENDIF
| |
-
Open a browser and verify that the file displays properly. In order to obtain full credit
for the lab you need to email your source code (.html file) to your grader.
|