html - uncaught reference error Function not defined Javascript -
simple program calculate periodic interest rate, function defined , called in button still error uncaught reference still shows. appreciated
function calculatepayment(){ var loanamount = document.getelementbyid("txtloanamount").value; var interestrate = document.getelementbyid("txtinterestrate").value; var amortiperiod = document.getelementbyid("txtyearlyperiod").value; var totalpayment; var numofmonths; //convert strings text box integers loanamount = parseint(loanamount); interestrate = parseint(interestrate); amortiperiod = parseint(amortiperiod); //validate input boxes ensure values added , follow application rules if (loanamount == null || loanamount == ""){ //alert("you must enter loan amount"); message = "you must enter loan amount"; document.getelementbyid("errormessage").innerhtml = message; }else if (interestrate == null || interestrate == ""){ //alert("you must enter interest rate"); message = "you must enter interest rate"; document.getelementbyid("errormessage").innerhtml = message; }else if (amortiperid == null || amortiperiod == ""){ //alert("you must enter interest rate"); message = "you must enter interest rate"; document.getelementbyid("errormessage").innerhtml = message; }else if isnan(loanamount || interestrate || amortiperiod){ message = "the values enter must numeric"; document.getelementbyid("errormessage").innerhtml = message; }else{ numofmonths = amortiperiod * 12; paymentrate = loanamount * interestrate / 100; totalpayment = paymentrate * numofmonths; message = totalpayment; document.getelementbyid("errormessage").innerhtml = message; } }
here html:
<tr> <td><input type="button" onclick="calculatepayment()" name="btncalculate" value="calculate payment"/></td><td><input type="button" name="btnclear" value="clear"/></td> </tr>
unexpected identifier:
} else if isnan(loanamount || interestrate || amortiperiod) {
with markup (since provide less needed)
<input id="calcme" type="button" name="btncalculate" value="calculate payment" /> <input type="button" name="btnclear" value="clear" /> <input type="text" id="txtinterestrate" value="10">int <input type="text" id="txtloanamount" value="1200">amt <input type="text" id="txtyearlyperiod" value="1"> years per <div id="errormessage"> errors </div>
you need revise partially working code solve real problems here:
function showerror(message) { document.getelementbyid("errormessage").innerhtml = message; } function showloanamount(amount) { document.getelementbyid("txtloanamount").value = amount; } function showinterestrate(rate) { document.getelementbyid("txtinterestrate").value = rate; } function showyearlyperiod(period) { document.getelementbyid("txtloanamount").value = period; } function getloanamount() { return parseint(document.getelementbyid("txtloanamount").value); } function getinterestrate() { return parseint(document.getelementbyid("txtinterestrate").value); } function getyearlyperiod() { return parseint(document.getelementbyid("txtloanamount").value); } function calculatepayment() { // values var loanamount = getloanamount(); var interestrate = getinterestrate(); var amortiperiod = getyearlyperiod(); var totalpayment; var numofmonths; var message = ""; //set values showloanamount(loanamount); showinterestrate(interestrate); showyearlyperiod(amortiperiod); //validate input boxes ensure values added , follow application rules if (!loanamount) { message = "you must enter loan amount"; showerror(message); } if (!interestrate) { message = "you must enter interest rate"; showerror(message); } if (!amortiperiod) { message = "you must enter interest rate"; showerror(message); } if (isnan(loanamount) || isnan(interestrate) || isnan(amortiperiod)) { message = "the values enter must numeric"; showerror(message); } // crappy math here numofmonths = amortiperiod * 12; paymentrate = loanamount * interestrate / 100; totalpayment = paymentrate * numofmonths; message = totalpayment; showerror(message); } var el = document.getelementbyid("calcme"); el.addeventlistener("click", calculatepayment, false);
Comments
Post a Comment