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 6 of 6

Thread: Pricing script

  1. #1
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Pricing script

    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.

  • #2
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,087
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Question

    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!

  • #3
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,553
    Thanks
    78
    Thanked 4,382 Times in 4,347 Posts
    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.
    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
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,553
    Thanks
    78
    Thanked 4,382 Times in 4,347 Posts
    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
    Code:
    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.
    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.

  • #6
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,087
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Lightbulb

    -----
    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.
    Code:
    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.


  •  

    Posting Permissions

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