如果Else输出相同的结果

时间:2013-04-17 17:07:42

标签: javascript if-statement

很抱歉再次打扰你,但我的代码似乎还有其他问题。它为某些输入输出相同的结果,我花了大约15分钟的时间试图找出问题所在。我会问我的导师,但我现在不在Uni。

再次,代码在下面,感谢您花时间忍受我。

//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);
}

2 个答案:

答案 0 :(得分: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”而不是“将x加5,然后将其乘以2”。

答案 1 :(得分:0)

我想将您的条件分组存在一些问题。所以请检查下面的条件,我认为这可能会解决您的问题。

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