0

したがって、>=largeOrder を使用した if ステートメントを除いてすべてが機能しており、割引は適用されません。また、請負業者である従業員の場合もそうではありません。従業員が請負業者である場合、従業員は請負業者の割引を受けることができます。しかし正規業者の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>
4

3 に答える 3

4

使う必要があると思いますif-else-if

修正された if ブロック

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ブロックです。

 if (typeOfCustomer == "contractor") {
    discount = contractorDiscount;
} else {
    discount = smallOrderDiscount;  // <== Here previous discount will be overridden if typeOfCustomer is not contractor
}
于 2013-10-26T22:20:23.113 に答える
2

問題は、顧客が請負業者でない限り、if毎回代わりにを使用していることですelse if。これにより、最後の if/else ステートメントが実行されます。discount = smallOrderDiscount;これは単なる論理エラーです。

于 2013-10-26T22:20:38.460 に答える
1

これを解決する最善の方法は、キーワードを追加することです

debugger;

それらの if ステートメントの直前。次に (ブラウザで) F12 キーを押してページをリロードします。次にコードを実行すると、ブレークポイントで停止することがわかります。その後、F12 ツールを使用してステップスルーし、何が起こっているかを確認できます。

于 2013-10-26T22:11:19.030 に答える