0

这应该是一个简单的订单计算器,但由于某种原因它无法正常工作。这是代码:

var name, product, price, discount, quantity;

var name = prompt("What is your name?", "Enter name");
var sentence = "Hello " + name + " please look through our available products and services before placing your order.";
alert(sentence);

var product = prompt("Please enter the name of the product you are looking to purchase from the table.", "Enter product");
var quantity = 1*prompt("How many " + product + " would you like to purchase?", "Enter quantity");

var cost = price * quantity;
var orderdiscount = price * discount * quantity;
var totalcost = cost - orderdiscount;

a = confirm(+ name + ", you ordered " + quantity + " of " + product + ". Is this correct?");

if (a) {
    total = cost - (price * discount * quantity);
        if (product === "ice cream cake") {
                price = 20;
                discount = .15;
        } else if (product === "ice cream cone") {
                price = 3;
                discount = .01;
        } else if (product === "small ice cream sundae") {
                price = 5;
                discount = .05;
        } else if (product === "large ice cream sundae") {
                price = 6;
                discount = .05;
        } else if (prompt = ("Sorry, " + name + ". You entered an invalid product. Refresh the page to reload and place the order again.")) {
        }
        }

else 
{
    alert("Refresh the page to reload and place a new order");
}

document.write("Thank you for placing an order with us, " + name + ".");
document.write("</br>");
document.write("The cost of buying " + quantity + " of " + product + " is " + cost + ".");
document.write("</br>");
document.write("The discount for this purchase is " + orderdiscount + ".");
document.write("</br>");
document.write("With the discount, your total order cost is " + totalcost + ".");

当我加载页面并输入所有必要信息时,它会返回:

“感谢您向我们下订单。购买 1 个冰淇淋蛋糕的成本为 NaN。此次购买的折扣为 NaN。加上折扣后,您的总订单成本为 NaN。”

应该有计算数字来代替那些 NaN。这段代码中的什么导致这种情况发生?

4

7 回答 7

7

在设置变量的值之前,您正在进行计算。

    var cost = price * quantity;  <-- calculation here
    ....

    total = cost - (price * discount * quantity);  <-- calculation here
    if (product === "ice cream cake") {
            price = 20;
            discount = .15;  <-- setting variable here
    }
于 2012-10-26T13:19:17.600 回答
2

我会说这是因为 的值price没有在任何地方设置,并且在使用时未定义。

因此,当您计算时,cost您将乘以price(未定义)quantity,因此您不会得到有效的结果

于 2012-10-26T13:19:42.290 回答
1

在返回值上使用 parseInt(..):

prompt("How many " + product + " would you like to purchase?", "Enter quantity")
于 2012-10-26T13:30:25.720 回答
1

主要问题我可以在为其分配值之前使用价格变量看到它的成本变量,首先只需简单地添加var cost = price * quantity;这种var cost = function(){return price*quantity;}; 方式,每次调用成本时它都会重新计算价格*数量,而不是在文件的顶部在您完成操作以获取价格之前。发生这种情况是因为直到这里if (product === "ice cream cake") { price = 20; discount = .15; } else if (product === "ice cream cone") { price = 3; discount = .01;价格和折扣都是空的。

于 2012-10-26T13:26:47.047 回答
0

您在cost设定价格之前进行计算。

于 2012-10-26T13:19:48.483 回答
0

price对于开球来说是未定义的,但即便如此,如果用户要订购a dozentwelve代替12他/她订购的任何物品,您仍然会得到一个NaNwhen quantity = 1*'a dozen';。检查用户输入始终是必要的

于 2012-10-26T13:22:20.713 回答
-1

是因为你没有公布价格吗?如果它不是您的用户所需的字段。把它藏起来。

约拿

于 2012-10-26T13:20:59.200 回答