这是作业,所以很清楚我要做什么。
编写一个程序,根据三个因素计算酒店房间的价格。首先,一个房间里会有多少人:1-2、3-4 或 5-6?一个房间最多允许六个人。二、客人是AAA会员吗?第三,他们想要一个有风景的房间吗?费率计算如下:
基本房价为:1-2人=$50/晚 3-4人=$60/晚 5-6人=$70/晚 AAA会员每晚基本房价有优惠:1-2人= 15% 3-4 人 = 10% 5-6 人 = 5% 执行所有其他计算后,景观客房每晚的费用增加 10%。
程序应提示用户输入所有内容,执行计算并输出每晚的总成本。建议至少使用一些嵌套的 if/else 结构来计算成本。
像 firebug 和 JSLint 这样的调试器没有任何帮助,我怀疑我只是在做一些完全错误的事情,尽管我以前没有遇到过“嵌套 if”逻辑分配的问题。无论如何,我是一个完整而彻底的新手。
当我为 numberOfGuests 输入 1、为tripleAStatus 输入 N 和为 roomView 输入 N 进行测试时,finalRate 返回为 isNaN(我知道这意味着不是数字),我不知道为什么。
//Variable Declaration
var numberOfPeople;
var tripleAStatus;
var roomView;
var discountRate;
var roomRate;
var finalRate;
var discountPercent;
//Input
numberOfPeople = prompt("How many guests will their be? 6 Maximum.");
tripleAStatus = prompt("Are you a AAA Member? Y/N.");
roomView = prompt("Would you like your room to have a view?");
//Logic
if ((numberOfPeople <= 2) && (numberOfPeople > 0)) {
roomRate = 50;
discountPercent = .15;
} else if ((numberOfPeople <= 4) && (numberOfPeople > 2)) {
roomRate = 60;
discountPercent = .10;
} else if ((numberOfPeople <= 5) && (numberOfPeople > 4)) {
roomRate = 70;
discountPercent = .5;
} else {
alert("Your number of guests must be at least 1 and no more than 6");
}
if (tripleAStatus = "Y") {
discountRate = roomRate - (roomRate * discountRate);
} else if (tripleAStatus = "N") {
discountRate = roomRate;
} else {
alert("You need to answer with either Y or N");
}
if (roomView = "Y") {
finalRate = (discountRate) + ((discountRate) * .10);
} else if (roomView = "N") {
finalRate = discountRate;
} else {
alert("You need to answer with either Y or N");
}
//Output
document.write("Total cost per night is " + "$" + finalRate);