是什么导致这回归NaN?

时间:2012-10-26 13:16:40

标签: javascript

这应该是一个简单的订单计算器,但由于某种原因,它只是不起作用。这是代码:

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。此代码中的内容导致了这种情况发生?

7 个答案:

答案 0 :(得分: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
    }

答案 1 :(得分:2)

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

因此,当您计算cost时,您将price(未定义)乘以quantity,因此您将无法获得有效结果

答案 2 :(得分: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;价格和折扣为空。

答案 3 :(得分:1)

对返回值:

使用parseInt(..)
prompt("How many " + product + " would you like to purchase?", "Enter quantity")

答案 4 :(得分:0)

您在设定价格之前计算cost

答案 5 :(得分:0)

price未定义为启动,但即便如此,如果用户要订购a dozentwelve而不是12他/她订购的任何商品,在NaN时,您仍会以quantity = 1*'a dozen';结束。检查用户输入始终是必需的

答案 6 :(得分:-1)

你是不是要宣布价格吗?如果它不是您的用户所需的字段。隐藏它。

约拿