gpt4 book ai didi

javascript - 为什么我的计划没有给我分配的折扣?

转载 作者:行者123 更新时间:2023-11-28 11:57:56 25 4
gpt4 key购买 nike

因此,除了我的 if 语句与 >=largeOrder 之外,一切正常,它不会加入折扣。对于承包商雇员来说也不会。我们必须做出决定,为每个人提供最好的折扣,如果员工是承包商,他将获得承包商折扣。然而,常规承包商的 if 声明是完美的并且工作正常。所有输出都是正确的。所以我猜测它与我的嵌套 if 语句有关......

//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>

最佳答案

我认为你需要使用if-else-if

修改了 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;
}

根据我的理解,代码中的问题是最后一个 if block 。

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

关于javascript - 为什么我的计划没有给我分配的折扣?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19612432/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com