JavaScript- Drinking Age

R

Rich

Hello,

I'm learning JavaScript.........or Trying to and am stuck.

My old brain is just not understanding why this isn't working.

Internet Explorer reports an error but the error is Cryptic to me.

Any Help would be Greatly Appreciated.

THANKS!

My Goal
-------------------------------------------------
Write a set of functions that check for a legal drinking age.
I want it to include a data validation function(s) which will check the date
and prompt the user if the date is entered incorrectly or contains invalid
characters.
------------------------------------------------------------------------

My Broken Script
==========================================
<HTML>
<HEAD>
<TITLE>Legal Drinking Age Meter </TITLE>
</HEAD>
<BODY>
<script language="JavaScript" type="text/JavaScript">
var mm = prompt('What month were you born in?','1-12');
var bday = prompt('What day were you born on?','1-31');
var byear = prompt('What year were you born in?','1975');
document.write("<b>You were born on "+bmonth+" "+bday+", "+byear+".
("+mm+"/"+bday+"/"+byear+")</b>");
document.write("<br><br>I am "+yourage+" years old, and will turn "+age2+"
in:");
var ageString;
var legalAge=21;
var ageNumber;
ageString = prompt ("Please enter your age", " ");
ageNumber = parseInt(ageString);
document.write(ageString + "<br>");
document.write(ageNumber + "<br>");
document.write(legalAge + "<br>");
if (ageNumber >= legalAge)
{alert ("Let's Party!");}
else if (ageNumber < legalAge)
{alert ("Sorry! You're too young to drink!");}
else if (ageNumber < 0)
{alert ("Please enter a valid age"); }
else
{alert ("Please enter a valid age"); }

alert ("end of script");

</SCRIPT>

</BODY>
</HTML>
 
D

Dr John Stockton

JRS: In article <7PA1e.15909$Ax.7921@trnddc04>, dated Sun, 27 Mar 2005
16:03:15, seen in Rich
Hello,

I'm learning JavaScript.........or Trying to and am stuck.

My old brain is just not understanding why this isn't working.

Internet Explorer reports an error but the error is Cryptic to me.

Since you have not bothered to tell us what the error is, or when it
occurs, or what version of IE it is, it's even less understandable by
us.

The message IE gives me is perfectly plain. You need to sort out usage
of bmonth and mm. Also of yourage. Also of age2.
Any Help would be Greatly Appreciated.

Read the newsgroup FAQ.

Don't allow your posting agent to break lines.


parseInt is not needed; and, if used, should be used properly.

document.write can take multiple comma-separated parameters; and can be
more legible that way - e.g.
document.write(ageString, "<br>");

Date inputs should be validated, so that Feb 30 cannot be entered.

Numeric dates should not be presented in FFF on the WWW.
 
R

Randy Webb

Rich said:
Hello,

I'm learning JavaScript.........or Trying to and am stuck.

My old brain is just not understanding why this isn't working.

Internet Explorer reports an error but the error is Cryptic to me.

Any Help would be Greatly Appreciated.

Make sure that you are aware that the "legal drinking age" is different
in different locations on Earth.
I want it to include a data validation function(s) which will check the date
and prompt the user if the date is entered incorrectly or contains invalid
characters.
------------------------------------------------------------------------

My Broken Script
==========================================
<HTML>
<HEAD>
<TITLE>Legal Drinking Age Meter </TITLE>
</HEAD>
<BODY>
<script language="JavaScript" type="text/JavaScript">

language is deprecated, drop its use. But its not related to your
problems/errors.
var mm = prompt('What month were you born in?','1-12');
var bday = prompt('What day were you born on?','1-31');
var byear = prompt('What year were you born in?','1975');
document.write("<b>You were born on "+bmonth+" "+bday+", "+byear+".
("+mm+"/"+bday+"/"+byear+")</b>");

You have the month you were born in to be prompted and put in the
variable "mm" but you refer to it in your document.write as bmonth.

Also, beware that month numbers in script start at 0, not 1. So January
is month 0, not month 1.
document.write("<br><br>I am "+yourage+" years old, and will turn "+age2+"
in:");

Here, you are attempting to write two variables : yourage and age2 :
before they are defined.
var ageString;
var legalAge=21;
var ageNumber;
ageString = prompt ("Please enter your age", " ");
ageNumber = parseInt(ageString);

ageNumber = +prompt(......);

is shorter, more efficient, and less error prone. The + will convert the
number from a string to a number. Also, parseInt, when not used with a
radix, can lead to errors.
document.write(ageString + "<br>");
document.write(ageNumber + "<br>");
document.write(legalAge + "<br>");
if (ageNumber >= legalAge)
{alert ("Let's Party!");}
else if (ageNumber < legalAge)
{alert ("Sorry! You're too young to drink!");}

Everything below here is redundant. Also, you check for the < first, and
then check to see if its less than 0. So if a user input an age as -8,
then they would get the alert "You're too young to drink" and then get
the alert to "Please enter a valid age".

There are more problems than what I wrote about, but fix those first and
then go from there.
 
E

Evertjan.

Randy Webb wrote on 28 mrt 2005 in comp.lang.javascript:
Make sure that you are aware that the "legal drinking age" is different
in different locations on Earth.

Strangerder and strangerder,

The legal drinking age generally starts at birth.

It ends according to Florida governors at contested times.
 
E

Evertjan.

Mr.Clean wrote on 28 mrt 2005 in comp.lang.javascript:
Low, blow... however, take your opinions on this to another group,
they don't belong here,

As I didin't state an opinion, I suppose you read your opinion [or the
opposit of that] in my words.

However, if you want my optinion [or if not], Randy's statement of
difference in "legal drinking age" wrongly presumes there is such a
fixedly defined legal thing.

Well, there is not. Overhere there is a minimum age for buying liquor or
beer, and a minimum age of being served.
The input value "age" depends on those.

The legality of drinking as such [water or otherwise] is quite something
else. Forced drinking is the opposite of drinking prohibition, and
unfortunatly those are in the news.

In computer languages, the definition of the input and the result are a
prerequisite for usefull programming.
just like Mr Grossi's numerous undesired posts.

Who the hell is that? [Rhetorical question. I don't want to know.]

However, posting on usenet is not only dependent on somone else's desire.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top