我有一个函数,它应该返回一个数字,但它返回NaN。这是代码
function priceCalc(cust, transit, passType){
var price = 0;
if (passType === "monthly"){
if (cust === "student" || cust === "elderly"){
price = transit.monthly / 2;
} else if (cust === "transit worker"){
price = 0;
} else {
price = transit.monthly;
}
} else if (passType === "pre paid"){
if (cust === "transit worker") {
price = Infinity;
} else {
value = prompt('How much would you like on your pass?');
price = parseInt(value);
}
}
return price;
};
price是返回时应该有一个数值的变量,例如当我传入学生为cust param,公共汽车为过境参数(每月属性为60),以及每月对于传递类型参数,它应该返回30,但我得到NaN。我正在对它进行Jasmine测试,这就是
describe('the application', function(){
describe('publicPrice function', function(){
it('takes in customer status, transit choice, and pass choice to calculate a price', function(){
expect(App.priceCalc('adult', 'commuter rail', 'monthly')).toBe(80);
expect(App.priceCalc('student', 'commuter rail', 'monthly')).toBe(40);
expect(App.priceCalc('elderly', 'subway', 'monthly')).toBe(35);
expect(App.priceCalc('transit worker', 'bus', 'monthly')).toBe(0);
});
});
});
如果重要的话,该功能是该模块的一部分
var App = (function(){
var Transport = function(mode, monthly, prepaid){
this.mode = mode;
this.monthly = monthly;
this.prepaid = prepaid;
};
var commuterRail = new Transport('commuter rail', 80, 5);
var bus = new Transport('bus', 60, 2);
var subway = new Transport('subway', 70, 3);
var customerStatus = prompt('Please enter your status. \nAdult \nElderly \nStudent \nTransit worker');
var transitInput = prompt('Please select your method of transport: \ncommuter rail \nbus \nsubway');
var passSelection = prompt('Please select a pass: \nMonthly \nPrepaid');
var transitMethod;
if(transitInput === "commuter rail"){
transitMethod = commuterRail;
} else if(transitInput === "bus"){
transitMethod = bus;
} else if (transitInput === "subway"){
transitMethod = subway;
}
console.log(transitMethod);
function priceCalc(cust, transit, passType){
var price = 0;
if (passType === "monthly"){
if (cust === "student" || cust === "elderly"){
price = transit.monthly / 2;
} else if (cust === "transit worker"){
price = 0;
} else {
price = transit.monthly;
}
} else if (passType === "pre paid"){
if (cust === "transit worker") {
price = Infinity;
} else {
value = prompt('How much would you like on your pass?');
price = parseInt(value);
}
}
return price;
};
var publicPrice = function(customerStatus, transitMethod, passSelection){
return priceCalc(customerStatus, transitMethod, passSelection);
};
priceCalc(customerStatus, transitMethod, passSelection);
return {
// publicPrice: publicPrice,
priceCalc: priceCalc
};
})();
答案 0 :(得分:3)
price = transit.monthly / 2;
在这里,您告诉解释器使用对象monthly
的属性transit
。在您的代码中,transit
是一个字符串,因此transit.monthly
的评估结果为undefined
undefined / 2
评估为NaN
我认为你的意思是传入你创建的变量对象而不是字符串。
答案 1 :(得分:0)
transit.monthly
可能是一个字符串,你应该在对其进行微积分之前解析它:
function priceCalc(cust, transit, passType){
var price = 0;
if (passType === "monthly"){
if (typeof transit.monthly === 'string') {
transit.monthly = Number(transit.monthly);
}
if (cust === "student" || cust === "elderly"){
price = transit.monthly / 2;
} else if (cust === "transit worker"){
price = 0;
} else {
price = transit.monthly;
}
} else if (passType === "pre paid"){
if (cust === "transit worker") {
price = Infinity;
} else {
value = prompt('How much would you like on your pass?');
price = parseInt(value);
}
}
return price;
};
答案 2 :(得分:0)
您的函数需要三个参数:
function priceCalc(cust, transit, passType){}
在测试中,您将字符串作为第二个参数transit
传递,但该函数被编写为传输是一个对象。所以在你的priceCalc
函数中执行:
price = transit.monthly / 2
价格设为NaN
在我看来,priceCalc函数需要一个Transport
对象。
答案 3 :(得分:0)
您正在将字符串传递到传输中,您可能想要传递类Transport的对象。
如果您添加此代码(您的代码):
if(transit === "commuter rail"){
transit = commuterRail;
} else if(transit === "bus"){
transit = bus;
} else if (transit === "subway"){
transit = subway;
}
到你的功能的开头,它可能会起作用。
答案 4 :(得分:0)
您需要将转换方法名称转换为相应对象的代码放入priceCalc
函数中:
function priceCalc(cust, transit, passType){
var price = 0;
if(transit === "commuter rail"){
transit = commuterRail;
} else if(transit === "bus"){
transit = bus;
} else if (transit === "subway"){
transit = subway;
}
if (passType === "monthly"){
if (cust === "student" || cust === "elderly"){
price = transit.monthly / 2;
} else if (cust === "transit worker"){
price = 0;
} else {
price = transit.monthly;
}
} else if (passType === "pre paid"){
if (cust === "transit worker") {
price = Infinity;
} else {
value = prompt('How much would you like on your pass?');
price = parseInt(value);
}
}
return price;
};
您目前使用transitInput
变量执行此操作的代码是无用的,因为该变量没有值。
答案 5 :(得分:0)
你得到NaN,因为你试图划分"通勤铁路"用2。
App.priceCalc('student', 'commuter rail', 'monthly')
- > price = transit.monthly / 2;
其中transit
是"通勤铁路" 。您应该使用具有月度属性的对象。