PDA

View Full Version : Pricing script

Branoic
12-21-2010, 01:22 AM
I will admit I am new at coding, but I am stuck and I'm sure it has an easy solution but I just cant find it. At the root of my problem is I cannot get an If, else statement to work properly, many time I have come very close only to have it bog down somewhere.

I have to write a Coffie House script and the requirements are as follows:
I serve 3 sizes: 8oz., 12oz. and 16oz.
Espresso costs \$1.40 It is served as a “shot”. Extra shots are .50
Latte and Cappuccino cost the same. They are however, served in ounce sized cups. An 8oz. costs \$1.95, a 12oz. - \$2.35 and a 16 oz. - \$2.75.
An Americano costs \$1.20 plus \$.30 for each additional 8 ounces ordered.

The final price should also take into account a state sales tax of 5.5%.

I have tried so many variations, it's getting difficult to continue. Every place I go to look at sample code is different and doesnt work. As I said, I need to take the order, and turn the response into an If, else. Yet I am having a hard time with it.

jmrker
12-21-2010, 01:38 AM
Post the attempt that you believe closest to what you are trying to achieve
or the attempt that you believe you best understand the underlying logic.

Give us something to look at rather than do your homework for you! :eek:

Branoic
12-21-2010, 01:54 AM
It's not so much that I want my work done for me, although I can understand why folks would think that. I'm sure I'm not the first one to post such a vague question, so I understand it.

I have tried multiple things, even 'switch case, and defining variables at the outset. My most simple code was:

--------
if (order = "espresso" )
price = 1.40

else (order = "latter" )
price = 1.50
--------

I was using this just as a set up to figure out how it would work. After this I did the tax equation

-----
total = price * .055 + price
-----

No, I havent figured out how to round the equation yet, but that doesnt matter yet.

What always happens is no matter what I type in for 'order', the price goes to 1.50 and I get a total of 1.58. The 1.40 never gets called, I get a feeling the code goes to the last math option. On top of that, I cannot figure out how to ask for the additional shots within the If, else. I could probably do that stuff afterward, but I'm having such a basic problem I cant move forward

Old Pedant
12-21-2010, 02:05 AM
Look here:
www.codingforums.com/rules.htm

See rule 1.5.

We *will* help you with your code, but you have to have made a reasonable effort, first, and you have to show us (a) your code and (b) what you believe the problem to be.

Old Pedant
12-21-2010, 02:08 AM
Oops...I'm slow.

Okay, for starters, in JavaScript the = operator *ALWAYS* means "assign the value on the right side to the variable on the left side." *ALWAYS*

So when you do

if (order = "espresso" )

you are *NOT* testing whether order is the same as "espresso". You are CHANGING the value of order *TO* espresso.

You *MUST* use == to do comparisons. MUST.

jmrker
12-21-2010, 02:15 AM
-----
total = price * .055 + price
-----

No, I havent figured out how to round the equation yet, but that doesnt matter yet.

Depending on if you need to use the value again, you can round to dollars and cents to a string display like this.

total = ((price * 0.055) + price).toFixed(2);

Makes the assumptions of:
1. price is a number
2. total becomes a string that you no longer need as a number.
(If needed, then create a temp variable for the \$.00 display.