Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-24-2013, 01:44 AM   PM User | #1
jocosity
New to the CF scene

 
Join Date: Jan 2013
Location: Mobile, Alabama
Posts: 1
Thanks: 1
Thanked 0 Times in 0 Posts
jocosity is an unknown quantity at this point
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 )
}
jocosity is offline   Reply With Quote
Old 01-24-2013, 01:47 AM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,645
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
This would be javascript, not java (java cannot switch on String datatypes).
Moving from java to javascript forum.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
jocosity (01-24-2013)
Old 01-24-2013, 02:19 AM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,200
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:15 PM.


Advertisement
Log in to turn off these ads.