JimRam
JimRam

Reputation: 27

Why is my program not giving me the discounts I have assigned?

So everything is working except for my if statement with the >=largeOrder it will not put in the discount. Nor will if for the employee that is a contractor. We have to have a decision there to give everyone the best discount and if the employee is a contractor he gets the contractor discount. However the regular contractor if statement is perfect and working fine. all outputs are correct for that. So im guessing its something with my nested if statements...

//assumptions
var employeeDiscount = .1;
var largeOrder = 800;
var largeOrderDiscount = .05;
var smallOrderDiscount = 0;
var contractorDiscount = .2;
var salesTax = .08;
var seniorTax = 0;
var seniorAge = 90;
var tax, typeOfCustomer, orderAmount, employeeContractor;
var discount, discountAmount, subtotal, finalPrice, taxTotal;


//input

age = prompt("How old is the customer", "");
age = parseInt(age);
orderAmount = prompt("How much is the order", "");
orderAmount = parseInt(orderAmount);
typeOfCustomer = prompt("What type of customer is it, regular, employee, or contractor", "");


//calculations

if (age >= seniorAge) {
    tax = seniorTax;
} else {
    tax = salesTax;
}
if (typeOfCustomer == "regular") {
    if (orderAmount >= largeOrder) {
        discount = largeOrderDiscount;
    } else {
        discount = smallOrderDiscount;
    }
}
if (typeOfCustomer == "employee") {
    employeeContractor = prompt("is the employee a contractor?", "");

    if (employeeContractor == "yes") {
        discount = contractorDiscount;
    } else {
        discount = employeeDiscount;
    }
}
if (typeOfCustomer == "contractor") {
    discount = contractorDiscount;
} else {
    discount = smallOrderDiscount;
}
taxTotal = orderAmount * tax;
discountAmount = orderAmount * discount;
subtotal = orderAmount - discountAmount;
finalPrice = subtotal + taxTotal;

//output

document.write("Order amount: $" + orderAmount);
document.write("<br>Discount amount: $" + discountAmount);
document.write("<br>Subtotal: $" + subtotal);
document.write("<br>Taxes: $" + taxTotal);
document.write("<br>Final Price is: $" + finalPrice);

// -->
</script>

Upvotes: 0

Views: 59

Answers (3)

orhanhenrik
orhanhenrik

Reputation: 1415

The problem is that you are using if every time, instead of else if, this makes your last if/else statement run discount = smallOrderDiscount; unless the customer is a contractor. This is simply a logic error.

Upvotes: 2

Satpal
Satpal

Reputation: 133403

I think you need to use if-else-if

Modified if block

if (typeOfCustomer == "regular") {
    if (orderAmount >= largeOrder) {
        discount = largeOrderDiscount;
    } else {
        discount = smallOrderDiscount;
    }
} else if (typeOfCustomer == "employee") {
    employeeContractor = prompt("is the employee a contractor?", "");

    if (employeeContractor == "yes") {
        discount = contractorDiscount;
    } else {
        discount = employeeDiscount;
    }
}  else  if (typeOfCustomer == "contractor") {
    discount = contractorDiscount;
} else {
    discount = smallOrderDiscount;
}

As per my understanding problem in your code is last if-block.

 if (typeOfCustomer == "contractor") {
    discount = contractorDiscount;
} else {
    discount = smallOrderDiscount;  // <== Here previous discount will be overridden if typeOfCustomer is not contractor
}

Upvotes: 4

Jim W
Jim W

Reputation: 5016

Best way to solve this is add the keyword

debugger;

just before those if statements. Then hit F12 (in the browser) and reload the page, you should find when you next run the code that you stop at a breakpoint, you can then use the F12 tool to step through and see what's happening.

Upvotes: 1

Related Questions