[英]JavaScript function is returning NaN
我有一個函數,它應該返回一個數字,但它返回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),並且每月傳遞一次鍵入param它應該返回30,但我得到NaN。 我正在對它進行茉莉花測試
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
};
})();
price = transit.monthly / 2;
在這里,您告訴解釋器monthly
使用對象transit
屬性。 在你的代碼transit
是一個字符串, transit.monthly
評估為undefined
undefined / 2
計算為NaN
我認為你的意思是傳入你創建的變量對象而不是字符串。
transit.monthly
可能是一個String,你應該在對它進行微積分之前解析它:
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;
};
您的函數需要三個參數:
function priceCalc(cust, transit, passType){}
在您的測試中,您傳遞一個字符串作為第二個參數transit
,但該函數被編寫為傳輸是一個對象。 所以當你做priceCalc
函數時:
price = transit.monthly / 2
價格設置為NaN
在我看來,priceCalc函數需要一個Transport
對象。
你將一個字符串傳遞給傳輸,你可能想傳遞一個類Transport的對象。
如果您添加此代碼(您的代碼):
if(transit === "commuter rail"){
transit = commuterRail;
} else if(transit === "bus"){
transit = bus;
} else if (transit === "subway"){
transit = subway;
}
到你的功能開始,它可能會工作。
您需要將轉換方法名稱轉換為相應對象的代碼放入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
變量執行此操作的transitInput
是無用的,因為該變量沒有值。
你得到NaN,因為你試圖將“通勤鐵路”划分為2。
App.priceCalc('student', 'commuter rail', 'monthly')
- > price = transit.monthly / 2;
transit
是“通勤鐵路” 。 您應該使用具有月度屬性的對象。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.