By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,776 Members | 1,400 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,776 IT Pros & Developers. It's quick & easy.

JavaScript- Drinking Age

P: n/a
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>
Jul 23 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Rich wrote:
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.

-------------------------------------------------
Write a set of functions that check for a legal drinking age.
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.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Jul 23 '05 #2

P: n/a
JRS: In article <7PA1e.15909$Ax.7921@trnddc04>, dated Sun, 27 Mar 2005
16:03:15, seen in news:comp.lang.javascript, Rich
<no************@verizon.net> posted :
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.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #3

P: n/a
Randy Webb wrote on 28 mrt 2005 in comp.lang.javascript:
Write a set of functions that check for a legal drinking age.


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.

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Jul 23 '05 #4

P: n/a
In article <Xn********************@194.109.133.29>, ex**************@interxnl.net says...
Randy Webb wrote on 28 mrt 2005 in comp.lang.javascript:
Write a set of functions that check for a legal drinking age.


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.

Low, blow... however, take your opinions on this to another group, they
don't belong here, just like Mr Grossi's numerous undesired posts.
Jul 23 '05 #5

P: n/a
Mr.Clean wrote on 28 mrt 2005 in comp.lang.javascript:
In article <Xn********************@194.109.133.29>,
ex**************@interxnl.net says...
Randy Webb wrote on 28 mrt 2005 in comp.lang.javascript:
>> Write a set of functions that check for a legal drinking age.
>
> 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.

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.

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Jul 23 '05 #6

P: n/a
In article <Xn********************@194.109.133.29>, ex**************@interxnl.net says...
Mr.Clean wrote on 28 mrt 2005 in comp.lang.javascript:
In article <Xn********************@194.109.133.29>,
ex**************@interxnl.net says...
Randy Webb wrote on 28 mrt 2005 in comp.lang.javascript:
>> Write a set of functions that check for a legal drinking age.
>
> 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.

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.


But you DID state your opinion on the matter in Florida. This is NOT a
groups to be doing that. take it elsewhere, 'nuf said!

Jul 23 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.