I have a function and it is supposed to return a number but instead it is returning NaN. Here is the code
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 is the variable that is supposed to have a number value when it is returned, for instance when I pass in student for the cust param, bus for the transit param(which has a monthly attribute which is 60), and monthly for the pass type param it should return 30, but instead I get NaN. I'm running a Jasmine test on it which is this
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);
});
});
});
And the function is part of this module if that matters
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
};
})();
App.priceCalc('student', 'commuter rail', 'monthly')You're passing the stringcommuter railfortransit, instead of an object.