Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    java script help

    I was hoping someone could help me fix this javascript code. I have been working for hours and it does not work.

    <html>

    <head>

    <title>Moondoe's Coffee House</title>

    </head>

    <script type="text/javascript">

    var drink, ounces;

    prompt( "Enter the drink type:espresso, latte, cappuccino, americano" );

    prompt( "Enter the ounce size: 8, 12, 16" );

    var shots = prompt ( "Enter the number of shots" );

    if ( drink == "espresso")

    price = 1.40;

    if ( drink == "latte") || drink == "cappuccino" )
    {
    if ( ounce == 8 )

    price = 1.95;

    else if ( ounce == 12 )

    price = 2.35;

    else if ( ounce == 16 )

    price = 2.75;
    }


    if ( drink == "americano" )

    price = 1.20 + ( ( (ounce/8) -1 ) * .30 );

    price = price + ( (shots-1) * .50 );

    price = price * 1.055;

    price = Math.round ( price*100 ) /100;

    alert ( "Your " + ounce + "oz. " + drink + "with " + shots + "\nshots of espresso costs: $ " + price );

    alert( "drink = " + drink + "ounce = "ounce + "shots = "shots );

    </script>

    </html>

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    3,020
    Thanks
    56
    Thanked 566 Times in 563 Posts
    what errors do you get when you look at it in firebug?

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,652
    Thanks
    80
    Thanked 4,640 Times in 4,602 Posts
    if ( drink == "latte") || drink == "cappuccino" )

    wrong. And you should know it is wrong, immediately, because there is one ( but two )'s. PARENTHESES COUNTS MUST MATCH. (As must [ ] and { }.)

    Also, the condition of an "if" must have parens around it.

    if ( ( drink == "latte") || ( drink == "cappuccino" ) )
    or simply
    if ( drink == "latte" || drink == "cappuccino" )
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,652
    Thanks
    80
    Thanked 4,640 Times in 4,602 Posts
    And there may be other errors, but as Xelawho asked: What errors do you see when you use a JavaScript debugger?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ive been able to change it to this.... but the last alert doesnt appear

    <html>

    <head>

    <title>Tristan's Coffee House</title>

    </head>

    <script type="text/javascript">

    var drink = prompt ( "Enter the drink type: espresso, latte, cappuccino, americano" );

    var drink= prompt ( "Enter the oz: 8, 12, 16" )

    var shots = prompt ( "Enter the number of shots" )

    if ( drink == "espresso")

    price = 1.40;

    if (( drink == "latte") || (drink == "cappuccino" ))

    if ( ounce == 8 )

    price = 1.95;

    else if ( ounce == 12 )

    price = 2.35;

    else if ( ounce == 16 )

    price = 2.75;


    if ( drink == "americano" )

    price = 1.20 + ( ( (ounce -8)/8 ) * .30 );

    (price = price * 1.055);

    Math.round ( price * "extra shot");

    alert ( "Your " + ounce + "oz. " + drink + "with " + shots + "\nshots of espresso costs: $ " + price );

    </script>

    </html>

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,652
    Thanks
    80
    Thanked 4,640 Times in 4,602 Posts
    This makes no sense:
    Code:
        Math.round ( price * "extra shot");
    You can't multiply price (which we assume to be number?) by a string.

    What are you hoping for? If price is 3, say, that you would get "extra shotextra shotextra shot" from the multiplication?

    Also, when you use Math.round(), you must *ASSIGN* the result to a variable. Math.round() does *NOT* magically change one or both of its arguments.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    <html>

    <head>

    <title>Tristan's Coffee House</title>

    </head>

    <script type="text/javascript">

    var drink, ounces;

    prompt ( "Enter the drink type: espresso, latte, cappuccino, americano" );

    prompt ( "Enter the oz: 8, 12, 16" )

    var shots = prompt ( "Enter the number of shots" )

    if ( drink == "espresso")

    price = 1.40;

    if (( drink == "latte") || (drink == "cappuccino" ))
    {
    if ( ounce == 8 )

    price = 1.95;

    else if ( ounce == 12 )

    price = 2.35;

    else if ( ounce == 16 )

    price = 2.75;
    }

    if ( drink == "americano" )

    price = 1.20 + ( ( (ounce -8)/8 ) * .30 );

    price = price + ( (shots-1) * .50 );

    price = price * 1.055;

    price = Math.round( price * 100 )/100;

    alert( "Your " + ounce + "oz. " + drink + "with " + shots + "shots of espresso costs: $ " + price );

    </script>

    </html>

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,652
    Thanks
    80
    Thanked 4,640 Times in 4,602 Posts
    You are close.

    Your code is going to charge for shots regardless of what drink is chosen. Is that what it is supposed to do?

    You really didn't need that Math.round() in there, though it's not "wrong" to use it.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •