Assignment 017

If Statement

This Web page prompts visitors for their age. Once the age is entered, it is sent to a JavaScript function located in the head of the Web page. If the age is less than sixteen, the function creates a response appropriate for that age. The response is displayed below:

Here's the code used to create the function in the head section of the Web page:

<script language="Javascript" type="text/javascript">

function checkage(age)
{
window.document.write("Welcome! ");

if (age<=16)
{
window.document.write("<h2>You're a little one and probably as smart as my dog!</h2>");
}

</script>

Here's the code used to prompt users for their age (located in the body of the document):

<script language="Javascript" type="text/javascript">

//declare variable to capture age of user
var age

//ask users how old they are
age=prompt("How old are you?", "");

//call function to check age
checkage(age);

</script>

Remember that the if statement is testing a condition. If the condtion is true, then the code inside the braces is run. Here are a few more examples of conditions:

if(age == 20)

if(age!=16)

Look at the comparison operators below to see the different ways to test a condition.