【发布时间】:2013-07-11 23:11:02
【问题描述】:
我目前正在学习编码课程,我们正在使用 JS。我的代码很好,但我遗漏了一些东西,因为对于折扣输出,我总是得到 NAN。有谁知道为什么会这样?
//Input
var orderAmount = prompt("What is the order amount?");
var contractor = prompt("Are you a contractor? yes/no?");
var employee = prompt("Are you an employee? yes/no?");
var age = prompt("How old are you?");
//Constant
var employeeDisc = .10;
var largeOrderDisc = .05;
var contractorDisc = .20;
var taxRate = .08;
var noTax = 0;
//Calculations
if (orderAmount >= 800) {
var discounta = orderAmount * largeOrderdisc;
}else {
var discounta = 0;
}
if (contractor == "yes") {
var discountc = orderAmount * contractorDisc;
}else if(contractor == "no") {
var discountc = 0;
}
if(employee == "yes") {
var discounte = orderAmount * employeeDisc;
}else if(emplyee == "no") {
var discounte = 0;
}
var discount = discountc + discounte + discounta;
var subtotal = orderAmount - discount;
if (age >= 90){
tax = subtotal * noTax;
}else {
tax = subtotal * taxRate;
}
total = subtotal - tax;
//Output
document.write("Original Price: $" + orderAmount);
document.write("Discount: $" + discount);
document.write("Subtotal: $" + orderAmount);
document.write("Tax: $" + tax);
document.write("Final Price: $" + total);
document.write("Final Price: $" + total);
抱歉,代码无法编译。现在已修复。现在的问题是我的 document.write 没有写。
【问题讨论】:
-
好吧,
discountc、discounte或discounta是NaN。 -
你不应该那样比较,先转换成int:
"15" > "10" -> true; "15" > "9" -> false -
请发布工作代码(当前代码甚至无法编译)并提供一个小提琴以获得更多有用的答案。
-
以上所有内容的补充:
if (contractor == "yes") } else if (contractor == "no") {- 如果用户在提示框中输入fgsfds怎么办? (是的,用户可能是这个愚蠢的)。
标签: javascript