JavaScript Errors - By the Numbers
(Page 4 of 4 )
You could do the above with numbers as well. Say you had a virtual lawn. Maybe you are eighty years old and you wear your pants up to your neck. You probably sit on your virtual porch all day people watching. Maybe some of those people get out of line. Here is a crotchety program to handle just such a situation:
<html>
<body>
<script type="text/javascript">
var age=prompt("Enter your age whippersnapper:","")
try
{
if(age>21)
throw "FirstGrump"
else if(age<21)
throw "SecondGrump"
else if(age==21)
throw "FourthGrump"
else if(isNaN(age))
throw "ThirdGrump"
}
catch(er)
{
if(er=="FirstGrump")
alert("I remember when I was your age...<falls asleep> zzzzzz")
if(er == "SecondGrump")
alert("Pull up your pants! And Get off my lawn!")
if(er == "ThirdGrump")
alert("Eh?!? Wassat?!? I didn't here ye")
if(er == "FourthGrump")
alert("Let's go get some booze!")
}
</script>
</body>
</html>
This program asks the young whippersnapper to enter their age. If the age is greater than 21, an exception is thrown and a pop-up appears saying, “I remember when I was your age <falls asleep> zzzzzz” If the age is less than 21, an exception is thrown that says:”Pull up your pants! And get off my lawn!” If the user inputs 21 as their age, a pop up displays exclaiming: “Let's go get some booze!” Finally, if the user inputs any other data, they will get an alert stating: “Eh?!? Wassat?!? I didn't here ye”
That concludes this issue. Next episode we will discuss JavaScript Objects and learn how to use them.
Till then...
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |