12.6 Homework Assignment

Objective:

Students will create a JavaScript script that prompts the user for their age and uses if-else statements to determine what legal activities the user can participate in, such as voting, driving, drinking alcohol, etc.

Task:

  1. Write a JavaScript script that:

    • Prompts the user to input their age using prompt().

    • Uses if-else statements to determine the user's eligibility for certain legal activities based on their age.

    • Outputs the results using console.log() to display the legal activities the user can participate in.

  2. Example Activities to check for:

    • Vote (18 and above)

    • Drive (16 and above)

    • Religion (20 and above)

    • Drink alcohol (21 and above)

    • Senior citizen benefits (65 and above)

Step-by-Step Instructions:

  1. Create an HTML file (e.g., age_eligibility_homework.html) and write the following script to ask for the user's age and determine eligibility for various activities.

    Example Code:

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Legal Activities Eligibility</title>
    </head>
    <body>
      <h1>Check the Console for Eligibility Results</h1>
      
      <script>
        // Ask the user for their age
        let age = prompt("Please enter your age:");
        age = parseInt(age);  // Convert the input to an integer
    
        // Check eligibility for various activities
        if (age >= 65) {
          console.log("You are eligible for senior citizen benefits.");
        } 
        
        if (age >= 21) {
          console.log("You are eligible to drink alcohol.");
        } 
        
         if (age >=20) {
          console.log("You are eligible to have religion.");
        } 
        
        if (age >= 18) {
          console.log("You are eligible to vote.");
        } 
        
        if (age >= 16) {
          console.log("You are eligible to drive.");
        } 
        
        if (age < 16) {
          console.log("You are not eligible to drive, vote, have religion or drink.");
        }
      </script>
    </body>
    </html>
  2. Open the file in a browser:

    • Open the HTML file in a browser and use Developer Tools (F12 or right-click and select Inspect > Console) to view the results after entering an age.

  3. Testing:

    • Input different ages (e.g., 12, 16, 18, 21, 65) and observe how the script outputs the appropriate messages for each legal activity.

    • Ensure that the script correctly handles different age groups and displays the corresponding activities.

Challenge:

  • Modify the script to include additional activities and show the result in web page document using id in div element , such as:

    • Working part-time (14 and above).

    • Running for president (35 and above).

    Example Code for Challenge:

    if (age >= 35) {
      console.log("You are eligible to run for president.");
    } 
    
    if (age >= 14) {
      console.log("You are eligible to work part-time.");
    }

Homework Submission:

  • Save your HTML file and submit it to the instructor.

  • Ensure that the script runs correctly and outputs the appropriate messages based on the age entered.

Expected Outcome:

Students will demonstrate their understanding of if-else statements by writing a script that checks user input and determines eligibility for various legal activities. They will also practice using prompt() and console.log() to interact with the user and display results.

Last updated