In this project, you will write a JavaScript program that calculates movie ticket prices based on the user’s age.
You will use iteration and conditional logic to determine which price category applies and display the result on the webpage.
By the end of this lesson, you should be able to:
- Use variables and conditional logic to implement decision-making in JavaScript.
- Capture user input from a text field using
document.getElementById(). - Update the DOM dynamically to display results.
- Understand basic iteration concepts through repetitive checking and validation.
Include the following elements in your index.html:
<input id="age" type="number" placeholder="Enter your age">
<button onclick="calculate()">Check Ticket Price</button>
<p id="priceOutput"></p>Your code should define a function named calculate() that reads the age from the input, determines the correct ticket price, and displays it.
function calculate() {
// your code here
}| Age Range | Ticket Type | Price |
|---|---|---|
| 0–12 | Child | $7 |
| 13–17 | Student | $12 |
| 18–64 | Adult | $20 |
| 65+ | Senior | $12 |
- The user enters their age in the input field.
- When the button is clicked, your program calls the
calculate()function. - The function checks the input against the price ranges and prints the correct ticket price inside the
<p id="priceOutput"></p>element.
| Criteria | Description | Points |
|---|---|---|
| HTML Setup | Includes input with id="age" and output element with id="priceOutput" |
20 |
| JS Functionality | Implements calculate() correctly using conditional statements |
40 |
| Output Accuracy | Displays correct prices for all age groups | 30 |
| Code Clarity | Proper indentation, comments, and readability | 10 |
- Forgetting to convert the input value to a number using
Number(). - Not assigning
idattributes correctly in HTML. - Printing output to the console instead of the webpage.
- Missing
calculate()definition or incorrect function name.
| Input (Age) | Expected Output |
|---|---|
| 5 | $7 |
| 15 | $12 |
| 35 | $20 |
| 70 | $12 |
- Unit 8.3 – Iteration introduces using loops and conditions to automate repetitive logic.
- This lab focuses on using if–else structures to apply different rules for different input cases.
- This lays the groundwork for more complex iteration problems, such as validating multiple inputs or processing lists of data.
Commit and push your work to your assigned GitHub Classroom repository.
Your instructor’s autograder will test your project automatically.