JavaScript function is returning NaN -
i have function , supposed return number instead returning nan. here 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 on pass?'); price = parseint(value); } } return price; };
price variable supposed have number value when returned, instance when pass in student cust param, bus transit param(which has monthly attribute 60), , monthly pass type param should return 30, instead nan. i'm running jasmine test on this
describe('the application', function(){ describe('publicprice function', function(){ it('takes in customer status, transit choice, , pass choice calculate 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 function part of module if 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 status. \nadult \nelderly \nstudent \ntransit worker'); var transitinput = prompt('please select method of transport: \ncommuter rail \nbus \nsubway'); var passselection = prompt('please select 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 on 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;
here telling interpreter use property monthly
of object transit
. in code transit
string transit.monthly
evaluates undefined
undefined / 2
evaluates nan
i think mean pass in variable objects created instead of string.
Comments
Post a Comment