gpt4 book ai didi

javascript - 当我需要一个数字时,JS 变量输出为 isNaN

转载 作者:行者123 更新时间:2023-12-02 19:17:37 25 4
gpt4 key购买 nike

这是作业,因此很清楚我要做什么。

Write a program that calculates the price of a hotel room based on three factors. First, how many will be in one room: 1-2, 3-4, or 5-6? No more than six are allowed in a room. Second, are the guests members of AAA? Third, do they want a room with a view? The rate is calculated as follows:

Basic room rate is: 1-2 people = $50/night 3-4 people = $60/night 5-6 people = $70/night There is a discount for AAA members off of the basic room rate per night: 1-2 people = 15% 3-4 people = 10% 5-6 people = 5% A room with a view costs 10% more per night after all other calculations are performed.

The program should prompt the user for all the inputs, perform the calculations and output the total cost per night. It is suggested to use at least some nested if/else structures to calculate the cost.

像 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);

最佳答案

看起来像

discountRate = roomRate - (roomRate * discountRate);

应该阅读

discountRate = roomRate - (roomRate * discountPercent);

这就是为什么你在 FinalRate 中得到 NaN 的原因;此时尚未定义discountRate,因此您的代码实际上为discountRate = 1 - (1 * undefined)

正如其他发帖者所提到的,您还需要更改条件以使用 ====== 是一个赋值运算符,因此您实际上不是检查 tripleAStatus 是否为 "Y",而是检查 "Y" 计算结果为 true (它总是如此)。

tripleAStatus = 'N';
if (tripleAStatus = 'Y') {
// tripleAStatus is now "Y", and the code inside this if will always be executed
}

工作变更:http://jsfiddle.net/5ZVkE/

关于javascript - 当我需要一个数字时,JS 变量输出为 isNaN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12945086/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com