user4592091
user4592091

Reputation:

JavaScript Forms - Applying a Discount for a Certain Quantity

I am creating a simple form that calculates price based on quantity ordered and tax. I also want to apply a discount for the following quantities:

10-19: 10% discount
20-29: 20% discount
30-39: 30 % discount
40-99: 40 % discount

My issue is that the function still returns the total price without the discount for a certain quantity. When the quantity entered is in a certain range, I have set it up so that the discount updates to the appropriate percentage (var discountPrice). If a discount is applied, then the total should update to the discountedTotal, and calculate the tax and final total from there. However, there seems to be an issue with my syntax since none of this is being applied to the function when I run it.

Any insight as to why the if/else statement or the function as a whole is not running properly would be appreciated. If needed, here is the full HTML/JS: http://jsfiddle.net/pas0tmpL/

function priceCalculation() {

var nameFirst = document.getElementById("first_name").value;
var nameLast = document.getElementById("last_name").value;
var phoneNum = document.getElementById("phone_number").value;
var quantity = document.getElementById("quantity_order").value;
var price = document.getElementById("price_fixed").value;
var total = quantity * price;
var discountPrice = 0
var discountedTotal = total - (total * discountPrice);
const taxRate = 0.085;
var tax = total * taxRate;
var totalPlusTax = total + tax;

if (quantity > 9 || quantity < 20) {
        discountPrice = .10;        
        total = discountedTotal;
    }           
    else if (quantity > 19 || quantity < 30) {
        discountPrice = .20;
        total = discountedTotal;
    }
    else if (quantity > 29 || quantity < 40) {
        discountPrice = .30;
        total = discountedTotal;    
    }
    else if (quantity > 39 || quantity < 100) {
        discountPrice = .40;
        total = discountedTotal;    
    }

document.getElementById("order_total").value = "$" + totalPlusTax.toFixed(2);

Upvotes: 1

Views: 9081

Answers (2)

ber2008
ber2008

Reputation: 323

Like this:

function priceCalculation() {
var nameFirst = document.getElementById("first_name").value;
var nameLast = document.getElementById("last_name").value;
var phoneNum = document.getElementById("phone_number").value;
var quantity = document.getElementById("quantity_order").value;    
var price = document.getElementById("price_fixed").value;
var total = quantity * price;
var discountPrice = 0  ;
var discountedTotal = 0;
const taxRate = 0.085;
var tax = total * taxRate;
var totalPlusTax = total + tax;

   if (quantity > 9 || quantity < 20) {
        discountPrice = .10;        
        total = total - (total * discountPrice);
    }           
    else if (quantity > 19 || quantity < 30) {
        discountPrice = .20;
        total = total - (total * discountPrice);
    }
    else if (quantity > 29 || quantity < 40) {
        discountPrice = .30;
        total = total - (total * discountPrice);    
    }
    else if (quantity > 39 || quantity < 100) {
        discountPrice = .40;
        total = total - (total * discountPrice);    
    }

document.getElementById("order_total").value = "$" + totalPlusTax.toFixed(2);

} // end function priceCalculation();    

Upvotes: 2

Eric Hughes
Eric Hughes

Reputation: 831

You have several problems that I can see. One is, you calculate the discountedTotal at the beginning using

var discountPrice = 0
var discountedTotal = total - (total * discountPrice);

total - (total * 0) will just give you total. You should probably only set discountedTotal after you know what your discountPrice is...

Your if statements have some problems also. As a hint, || means "or". Thus your first if is saying "if the quantity is more than 9 or less than 20"--and every integer in existence is either more than 9 or less than 20 (some are both!). So you'll only ever attempt to apply the first discount, and that to everything.

Good luck!

Upvotes: 0

Related Questions