2012-10-26 142 views
0

这应该是一个简单的订单计算器,但由于某种原因,它只是不起作用。下面的代码:这是什么导致它返回NaN?

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

只是一个字:调试 – akonsu

+4

你自己有值之前'price'做了很多的计算和'discount'。 –

+0

'var price;'相当于'var price = NaN;',反过来说任何乘以NaN的数字都是NaN,因此'var cost = price * quantity'将始终为NaN。只需给'价格'和'折扣'一个数字值,即使它是0. – Mahn

回答

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 
    } 
+0

大声笑..你的目标是关闭;-) – musefan

2

我会说这是因为price值没有在任何地方设置和使用它的时间没有定义。

因此,当你计算cost你乘以price(这是不确定的)由quantity,因此您设定的价格之前,你不会得到有效的结果

+0

它被定义在脚本的顶部。 – undefined

+0

@xyu:是啊是啊是...编辑 – musefan

-1

难道你没有宣布价格?如果它不是您的用户所需的字段。把它藏起来。

约拿

+0

var name,product,price,discount,quantity;'存在于文件的顶部。 –

0

price是不确定的开球,但即便如此,如果用户是订购a dozentwelve,而不是他/她的订单无论项目的12,你还是会最终有一个NaNquantity = 1*'a dozen';。检查用户输入将永远是一个必然

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;价格和折扣为空。

1

使用parseInt函数(..)的返回值:

prompt("How many " + product + " would you like to purchase?", "Enter quantity") 
相关问题