5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed
If conditions
1. The if condition
While writing a program, there may be a situation when you
need to adopt one path out of the given two paths. So you
need to make use of conditional statements that allow your
program to make correct decisions and perform right actions.
JavaScript supports conditional statements which are used to
perform different actions based on different conditions.
JavaScript supports following forms of if..else statement:
if statement
if...else statement
if...else if... statement.
2. Copy the following program into
dreamweaver and test it.
<html>
<body>
<script type = "text/javascript">
var age = prompt ("what is your age?","");
age = parseInt(age);
if (age >65)
{
document.write ("you are a senior citizen");
}
</script>
</body>
</html>
3. Add this piece of code into the
program.
else (age <65)
{
document.write ("you are not a senior citizen");
}
4. Make your own quiz by changing the following code
into your own question and answers
<script type = “text/javascript”>
var answer1=prompt('How many moons does Earth have?','');
if (answer1 == 1 ) {
alert('Correct!');
} else {
alert('Sorry. The correct answer is 1');
}
var answer2=prompt('How many moons does Saturn have?','');
if (answer2 == 31) {
alert('Correct!');
} else {
alert('Sorry. The correct answer is 31');
}
</script>