JavaScript- Drinking Age

Discussion in 'Javascript' started by Rich, Mar 27, 2005.

  1. Rich

    Rich Guest

    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>
     
    Rich, Mar 27, 2005
    #1
    1. Advertising

  2. JRS: In article <7PA1e.15909$Ax.7921@trnddc04>, dated Sun, 27 Mar 2005
    16:03:15, seen in news:comp.lang.javascript, Rich
    <> 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.
     
    Dr John Stockton, Mar 27, 2005
    #2
    1. Advertising

  3. Rich

    Randy Webb Guest

    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
     
    Randy Webb, Mar 27, 2005
    #3
  4. Rich

    Evertjan. Guest

    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)
     
    Evertjan., Mar 28, 2005
    #4
  5. Rich

    Evertjan. Guest

    Mr.Clean wrote on 28 mrt 2005 in comp.lang.javascript:
    > In article <Xns96275633D7716eejj99@194.109.133.29>,
    > 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)
     
    Evertjan., Mar 28, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. RickB
    Replies:
    0
    Views:
    468
    RickB
    Feb 6, 2004
  2. Replies:
    6
    Views:
    341
    Mike Schilling
    Jan 14, 2005
  3. =?iso-8859-1?B?bW9vcJk=?=
    Replies:
    7
    Views:
    827
    Roedy Green
    Jan 2, 2006
  4. cylin
    Replies:
    6
    Views:
    576
    Mike Wahler
    Aug 19, 2003
  5. kenny
    Replies:
    0
    Views:
    220
    kenny
    Dec 5, 2010
Loading...

Share This Page