...

View Full Version : java script help



aawermeling21
12-15-2011, 09:56 PM
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>

xelawho
12-15-2011, 10:17 PM
what errors do you get when you look at it in firebug?

Old Pedant
12-15-2011, 10:19 PM
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" )

Old Pedant
12-15-2011, 10:20 PM
And there may be other errors, but as Xelawho asked: What errors do you see when you use a JavaScript debugger?

aawermeling21
12-16-2011, 11:09 PM
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>

Old Pedant
12-16-2011, 11:19 PM
This makes no sense:

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.

aawermeling21
12-16-2011, 11:34 PM
<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>

Old Pedant
12-17-2011, 02:57 AM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum