0

我这里有点麻烦。我检查以确保我的变量只用数字定义,但代码仍然不是数字返回。有人知道我在做什么错吗?

下面的代码:

//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 两次吗?

4

3 回答 3

3

NaN是当Number操作给出无效结果时得到的结果(不是数字)

喜欢:

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

NaN从某种意义上说是有毒的,任何NaN作为操作数的操作也会返回NaN。找出你的坏操作在哪里。

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

于 2013-04-17T16:16:02.513 回答
2

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

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

//declare vars and/or constants
var endString = "Total cost per night is $";
var viewCost = 0;
var discount = 0;
于 2013-04-17T16:19:25.150 回答
1
var viewCost; // <---defined, but not assigned to

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

在您第一次运行时,您只声明了变量,但尚未分配给它:

于 2013-04-17T16:18:16.667 回答