473,405 Members | 2,210 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

JavaScript- Drinking Age

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
6 2350
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.