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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jan 2013
    Location
    Mobile, Alabama
    Posts
    1
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Need a little help with a discount code

    Hey everyone,

    I'm totally new to this and learning as I go - so please bare with me!

    I need to add a discount code to my website and I can't seem to get it right.
    Using the code "JoeCain" should award the customer a $25 gift certificate.
    I tried my best to write the code, but my total amounts to $25 and not the price minus $25.

    Here's what I have..(ignore the 'frenchfry' code, that one was written by the man who helped with my site)

    switch(discount) {
    case 'frenchfry':
    discount_amount = 0.10
    price = price - (price*discount_amount)
    discount_applied = ' <i>(Applied 10% Off)</i>'
    break;
    case 'JoeCain':
    discount_amount = 25
    price = price - (price-discount_amount)
    discount_applied = ' <i>(Applied $25 Off)</i>'
    break;
    }

    $('#price').html('Price: $'+price+'.00' + discount_applied )
    }

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    This would be javascript, not java (java cannot switch on String datatypes).
    Moving from java to javascript forum.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • Users who have thanked Fou-Lu for this post:

    jocosity (01-24-2013)

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,947
    Thanks
    79
    Thanked 4,424 Times in 4,389 Posts
    Wrong:
    price = price - (price-discount_amount)

    Right:
    price = price-discount_amount;

    Or, more succinctly:
    price -= discount_amount;

    By the same token, this code:
    discount_amount = 0.10
    price = price - (price*discount_amount)

    Could be written:
    discount_amount = 0.10;
    price -= (price*discount_amount);

    Or, even simpler,
    discounted_rate = 0.90;
    price *= discounted_rate;

    Please be sure to put a semicolon at the end of every JavaScript statement.

    No, it's not required. But there are a few cases where what YOU think is the end of the statement turns out not to be where JS thinks it is. Play it safe.
    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
    •