-1

很抱歉再次打扰大家,但我的代码似乎还有另一个问题。它为某些输入输出相同的结果,我在过去 15 分钟左右试图找出问题所在。我会问我的导师,但我目前不在大学。

再次,代码在下面,感谢您抽出宝贵的时间来忍受我。

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

//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
//double yes

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

2 回答 2

2

运算符比&&绑定得更紧密||,这意味着

if(pplCount == "1" || pplCount == "2" && discYes == "Yes" && viewYes == "Yes") {

被解释为

if(pplCount == "1" || ((pplCount == "2" && discYes == "Yes") && viewYes == "Yes")) {

因此,当“pplCount”为 1 时,将采用该逻辑分支,而不管“discYes”和“viewYes”的值如何。

这就像普通的代数约定(纸和铅笔,忘记 JavaScript),其中乘法优先于加法。当你写

2x + 5

那么每个人都知道您的意思是“将 x 乘以 2,然后加 5”而不是“将 5 加到 x,然后将其乘以 2”。

于 2013-04-17T17:14:45.407 回答
0

我想对你的条件进行分组有一些问题。所以请检查您的条件,如下所示,我认为它可能会解决您的问题。

if((pplCount === "1" || pplCount === "2") && (discYes === "Yes" && viewYes === "Yes")) 
{
......
......
}
else if()
{
}
于 2013-04-17T17:18:11.173 回答