JavaScript conditionals exercise

Download this page and open with Dreamweaver. Or right click and select "View Source", then cut and paste the code to Dreamweaver.

Task 1

This task will use an if block to test the user's age.

Use the place indicated in the code for this JavaScript exercise.

Code a prompt() box to ask the user for their age. Load the value they return into a variable called age. Next use an if / else block to test age to see if the user is 18 years old or over. If they are 18 or over use an alert() to inform them that they may proceed. If they are not 18 or over use an alert() to advise them to leave your page.

Extension

  1. Extend you script with a third condition to see if the input supplied by the user is of the correct type (i.e. a number in the range 3 to 130 and not a word or any other form of non-usable input). Create an alert if this condition is triggered which prompts the user to enter only appropriate data.