NaN显然是一个数字,或者是它?

时间:2013-04-17 16:13:55

标签: javascript nan

我在这里遇到了一些麻烦。我检查以确保我的变量仅使用数字定义,但代码仍然返回而不是数字。谁知道我做错了什么?

以下代码:

//declare vars and/or constants
var endString = "Total cost per night is $";
var viewCost;
var discount;

//input vars
pplCount = prompt("How many people will be staying?");
discYes = prompt("Do you have an AAA discount?");
viewYes = prompt("Do you want a room with a view?");

//if variables

if(pplCount === "1" || pplCount === "2" && discYes === "Yes" && viewYes === "Yes") {
discount = 50 * 0.15;
roomCost = 50;
roomCost = +roomCost - discount + viewCost;
viewCost = (+roomCost * 0.10);
document.write(endString + roomCost);
}else if(pplCount === "3" || pplCount === "4" && discYes === "Yes" && viewYes === "Yes") {
discount = 60 * 0.10;
roomCost = 60;
roomCost = roomCost - discount + viewCost;
viewCost = (roomCost * 0.10);
document.write(endString + roomCost);
}
else if(pplCount === "5" || pplCount === "6" && discYes === "Yes" && viewYes === "Yes") {
discount = 70 * 0.05;
roomCost = 70;
roomCost = roomCost - discount + viewCost;
viewCost = (roomCost * 0.10);
document.write(endString + roomCost);
}

else if(pplCount === "1" || pplCount === "2" && discYes === "No" && viewYes === "Yes") {
discount = 50 * 0.15;
roomCost = 50;
roomCost = roomCost + viewCost;
viewCost = (roomCost * 0.10);
document.write(endString + roomCost);
}
else if(pplCount === "3" || pplCount === "4" && discYes === "No" && viewYes === "Yes") {
discount = 60 * 0.10;
roomCost = 60;
roomCost = roomCost + viewCost;
viewCost = (roomCost * 0.10);
document.write(endString + roomCost);
}
else if(pplCount === "5" || pplCount === "6" && discYes === "No" && viewYes === "Yes") {
discount = 70 * 0.05;
roomCost = 70;
roomCost = roomCost + viewCost;
viewCost = (roomCost * 0.10);
document.write(endString + roomCost);
}

else if(pplCount === "1" || pplCount === "2" && discYes === "No" && viewYes === "No") {
discount = 50 * 0.15;
roomCost = 50;
viewCost = (roomCost * 0.10);
document.write(endString + roomCost);
}
else if(pplCount === "3" || pplCount === "4" && discYes === "No" && viewYes === "No") {
discount = 60 * 0.10;
roomCost = 60;
viewCost = (roomCost * 0.10);
document.write(endString + roomCost);
}
else if(pplCount === "5" || pplCount === "6" && discYes === "No" && viewYes === "No") {
discount = 70 * 0.05;
roomCost = 70;
viewCost = (roomCost * 0.10);
document.write(endString + roomCost);
}

else if(pplCount === "1" || pplCount === "2" && discYes === "Yes" && viewYes === "No") {
discount = 50 * 0.15;
roomCost = 50;
roomCost = roomCost - discount;
viewCost = (roomCost * 0.10);
document.write(endString + roomCost);
}
else if(pplCount === "3" || pplCount === "4" && discYes === "Yes" && viewYes === "No") {
discount = 60 * 0.10;
roomCost = 60;
roomCost = roomCost - discount;
viewCost = (roomCost * 0.10);
document.write(endString + roomCost);
}
else if(pplCount === "5" || pplCount === "6" && discYes === "Yes" && viewYes === "No") {
discount = 70 * 0.05;
roomCost = 70;
roomCost = roomCost - discount;
viewCost = (roomCost * 0.10);
document.write(endString + roomCost);
}

是因为我两次定义roomCost吗?

3 个答案:

答案 0 :(得分:3)

NaNNumber操作提供无效结果(非数字)时获得的

像:

5 - "a"
5 + NaN
parseInt("a", 10)
5 + undefined

NaN 有毒,因为任何以NaN作为操作数的操作也会返回NaN。找出你操作不好的地方。

看着你的代码,我发现你用的是字符串而不是数字。使用parseInt()将字符串转换为正确的数字。如果parseInt返回NaN给出错误,请务必验证您的输入。

答案 1 :(得分:2)

忽略你在这里做的其他顽皮的东西,你在一些地方使用viewCost而没有先定义它。 (这是不是数字。)

将前几行更改为以下内容可解决问题:

//declare vars and/or constants
var endString = "Total cost per night is $";
var viewCost = 0;
var discount = 0;

答案 2 :(得分:1)

var viewCost; // <---defined, but not assigned to

roomCost = +roomCost - discount + viewCost;
                                  ^^^^^^^^

当你第一次运行时,你只是声明了变量,但尚未分配它: