我在這裏遇到了一些麻煩。我檢查確保我的變量僅用數字定義,但代碼仍然是不是數字。任何人都知道我在做什麼錯了?下面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?
問題是什麼/問題?它在代碼中是哪裏? –
當我運行該程序時,它工作正常,但endString變量打印出「每晚的總成本是$ NaN」,所以這意味着roomCost變量被讀取爲不是數字。 – minecraftModder
看起來像功課。 – Jeff