The IF Statement - 04if.flaThis program builds on the Maths example and validates the input using IF statements.
|
|||||||||||||||||||||||||||||||||||||||
Start up Macromedia Flash
This exercise adds to the maths exercise to validate the input data. |
|
||||||||||||||||||||||||||||||||||||||
Modify the code that handles the "Button Release" event. This code ensures that the first number you enter is really a number and also that it is positive. First you test for error conditions and if there are none, you carry out the intended action. Here is the code.
on (release)
{
// Test for non numeric input (Is Not a Number isNaN)
if (isNaN(_root.inputOne))
{
_root.outputText = "The first number you entered is not valid.";
}
else
// Test for negative input
if (_root.inputOne < 0)
{
_root.outputText = "The first number you entered must be positive.";
}
else
// Carry out the addition (only if there were no errors).
{
_root.outputText = _root.inputOne - - _root.inputTwo;
}
}
|
|||||||||||||||||||||||||||||||||||||||
Here is the finished program. You can download the source code. |
Test this program by typing text instead of numbers.
To run this again, refresh your page. |
||||||||||||||||||||||||||||||||||||||
Task
|
|
||||||||||||||||||||||||||||||||||||||
Notes
Examples
|
|||||||||||||||||||||||||||||||||||||||
© C N Bauers (2004) - You may use these materials for your self education. Educational institutions may use these materials too.