PDA

View Full Version : Why does my script return a NaN error (Not a number)



Marc312
Feb 27th, 2014, 09:45 PM
Dear all,

First of all, thanks for your help. I am somewhat a beginner in javascript although I have some knowledge of PHP. I am trying to code a calculator that recreates the PMT function. I started with a skeleton that I found online and I extend it little by little. I am currently trying to add the concept of Future Value to the code. Before I break it down and explain you the purpose of every part, here is the big chunk of coding:



<html>

<head>
<title>Calculatrice</title>
<script type='text/javascript' src='jquery.js?ver=1.3.2'></script>
<script type='text/javascript'>
$(document).ready(function () {

function showTab( name )
{
name = '#' + name;

$('div').not(name).hide();

$(name).show();
}

$('#dropdown').change( function() {

showTab( $( this ).val() );
});

showTab( $('#dropdown').val() );

});
</script>

</head>
<body>





<script type="text/javascript"><!--

function PMT(i, n, p, f) {
var pp = i * p * Math.pow((1 + i), n-1);
var dp = (1 - Math.pow((1 + i), n));
return pp/dp;


}


function CalculatePMTFromForm(idFuture, idFacteur, idLoanAmount, idPremierVersement, idAnnualInterestRate, idMonths, idResult) {

var b = jQuery('#' + idAnnualInterestRate).val()/100/365;
var n = jQuery('#' + idMonths).val();
var m = jQuery('#' + idFacteur).val();
var i = b*m;
var f = jQuery('#' + idFuture).val();
var pvers = jQuery('#' + idPremierVersement).val();
var ps = jQuery('#' + idLoanAmount).val();
var p = ps-pvers
var pmt = PMT(i, n, -p);
jQuery('#' + idResult).val(pmt.toFixed(2));
}
function performCalc() {
CalculatePMTFromForm('Future', 'Facteur', 'LoanAmount', 'PremierVersement', 'InterestRate', 'Months', 'Payment');
}
jQuery(document).ready(function() { performCalc(); jQuery('.calc').keyup(performCalc); });
// --></script></p>
<style type="text/css">
.entry .labeledform { text-align: left; }
.entry .labeledform label { width: 120px; text-align: right; float: left; padding-right: 10px; }
.entry .labeledform div { clear: left; }
</style>
<form class="labeledform">
<div><label for="Future">Future Value</label></p>
<input id="Future" class="calc" size="7" type="text" value="3" /> </div><br>
<div><label for="Facteur">Multiply</label></p>
<input id="Facteur" class="calc" size="7" type="text" value="30.8075" /> </div><br>
<div><label for="LoanAmount">Loan Amount</label></p>
<input id="LoanAmount" class="calc" size="7" type="text" value="145000" /> $</div><br>
<div><label for="PremierVersement">Down payment:</label></p>
<input id="PremierVersement" class="calc" size="7" type="text" value="0" /> $</div><br>
<div><label for="InterestRate">Interest Rate:</label></p>
<input id="InterestRate" class="calc" size="7" type="text" value="9" /> %</div><br>
<div><label for="Months">Number of Months:</label></p>
<input id="Months" class="calc" size="7" type="text" value="60" /> mois</div><br>

<div><label for="Payment">Loyer:</label></p>
<input id="Payment" size="7" type="text" value="0" /> $</div><br>
</div><br>
</form>


<br><br><h4>Facteur de multiplication selon le mois de l'ann&eacute;e.</h4><br>
Janvier: 30.8285<br>
F&eacute;vrier: 30.8075<br>
Mars: 30.8976<br>
Avril: 30.8765<br>
Mai: 30.8765<br>
Juin: 30.872<br>
Juillet: 30.8887<br>
Août: 30.8673<br>
Septembre: 30.8465<br>
Octobre: 30.8625<br>
Novembre: 30.8415<br>
D&eacute;cembre: 30.8575<br>
</body>
</html>


So what everything do? The point is that whenever I change the variables, the result (Payment) is automatically updated. Here are my different variables in the form:

Future Value --> This is the part I am struggling with. I want to be able to specify a future value for my calculator other than zero (which it is now).
Mulitply --> I need to calculate the interests on a daily basis so for instance if I divide my interest rate (say 9%) by 100, then 365 (days) * 30.8415, it would find me the exact, regular payments if the loan starts in November. Eventually, I will automatize that part but for now it will remain like that.
Loan Amount --> How much does the person borrow?
Down payment--> Say the person borrows $150K but makes a down payment of $5K, the actual financed amount is $145K not $150K.
Interest Rate --> Annual Interest rate (pretty much self explanatory)
Number of Months --> The loan is financed on how many months?
And finally Payment--> This field is automatically updated

Now I'll skip the rest of the explanation because I've identified exactly where the problem is. Whenever I change my function PMT () to add the variable f, it messes up my code and I get the answer that the Payment field (which is automatically updated) is NaN (not a number). Now, I've tried setting my PMT() function only to return var f to see what it is like and it tells me that it is 0. I get the point why, then, it tells me that the answer is not a number because I would need to divide a number by zero. The reason why it returns a zero even though I compute a different number is completely unknown to me.

In short, the calculation that is happening now:

function PMT(i, n, p, f) {
var pp = i * p * Math.pow((1 + i), n-1);
var dp = (1 - Math.pow((1 + i), n));
return pp/dp;


should be changed to this, so that Future Value is taken into account:


function PMT(i, n, p, f) {
pmt = ( i * ( p * Math.pow ( (i+1), n-1 ) + f ) ) / ( ( i + 1 ) * ( Math.pow ( (i+1), n) -1 ) );
return pmt;



and when I try to change it, it tells me that my new answer is NaN. Please help me
Sorry if some parts of the scripts are in French by the way :)

tl;dr: My script returns NaN and I don't know why.

rnd me
Feb 27th, 2014, 11:27 PM
here's a type-safe version of PMT, which should fix your problem:



function PMT(i, n, p, f) {
var pp = i * p * Math.pow((1 + +i), n-1);
var dp = (1 - Math.pow((1 + +i), +n));
return pp/dp;

}

note the typecasting to numbers using the "+" prefix operator to dis-ambiguate JS's shared addition/concat operator (+) when adding two strings.

Marc312
Mar 3rd, 2014, 02:28 PM
Hi, Thanks for your answer, I've tried implementing it but I am facing the same error message. It tells me that the answer is still not a number.

More specifically, when I try to change my PMT function for this:


function PMT(i, n, p, f) {
var pvif = Math.pow(1 + +i, +n);
var pmt = i / (pvif - 1) * -(p * pvif + +f);

return pmt;

}

I still have the same error message telling me that the answer is Not a Number. I've left the rest of the script unchanged but clearly the problem is around variable f.

For the reference, here is what the whole script looks like:



<html>

<head>
<title>Calculatrice</title>
<script type='text/javascript' src='jquery.js?ver=1.3.2'></script>
<script type='text/javascript'>
$(document).ready(function () {

function showTab( name )
{
name = '#' + name;

$('div').not(name).hide();

$(name).show();
}

$('#dropdown').change( function() {

showTab( $( this ).val() );
});

showTab( $('#dropdown').val() );

});
</script>

</head>
<body>





<script type="text/javascript"><!--
function PMT(i, n, p, f) {
var pvif = Math.pow(1 + +i, +n);
var pmt = i / (pvif - 1) * -(p * pvif + +f);

return pmt;

}




function CalculatePMTFromForm(idFuture, idFacteur, idLoanAmount, idPremierVersement, idAnnualInterestRate, idMonths, idResult) {

var b = jQuery('#' + idAnnualInterestRate).val()/100/365;
var n = jQuery('#' + idMonths).val();
var m = jQuery('#' + idFacteur).val();
var i = b*m;
var f = jQuery('#' + idFuture).val();
var pvers = jQuery('#' + idPremierVersement).val();
var ps = jQuery('#' + idLoanAmount).val();
var p = ps-pvers
var pmt = PMT(i, n, -p);
jQuery('#' + idResult).val(pmt.toFixed(2));
}
function performCalc() {
CalculatePMTFromForm('Future', 'Facteur', 'LoanAmount', 'PremierVersement', 'InterestRate', 'Months', 'Payment');
}
jQuery(document).ready(function() { performCalc(); jQuery('.calc').keyup(performCalc); });
// --></script></p>
<style type="text/css">
.entry .labeledform { text-align: left; }
.entry .labeledform label { width: 120px; text-align: right; float: left; padding-right: 10px; }
.entry .labeledform div { clear: left; }
</style>
<form class="labeledform">
<div><label for="Future">Future Value</label></p>
<input id="Future" class="calc" size="7" type="text" value="3" /> </div><br>
<div><label for="Facteur">Facteur de multiplication</label></p>
<input id="Facteur" class="calc" size="7" type="text" value="30.8075" /> </div><br>
<div><label for="LoanAmount">Montant du pr&ecirc;t:</label></p>
<input id="LoanAmount" class="calc" size="7" type="text" value="145000" /> $</div><br>
<div><label for="PremierVersement">Premier Versement:</label></p>
<input id="PremierVersement" class="calc" size="7" type="text" value="0" /> $</div><br>
<div><label for="InterestRate">Taux d'int&eacute;r&ecirc;t:</label></p>
<input id="InterestRate" class="calc" size="7" type="text" value="9" /> %</div><br>
<div><label for="Months">Nombre de mois:</label></p>
<input id="Months" class="calc" size="7" type="text" value="60" /> mois</div><br>

<div><label for="Payment">Loyer:</label></p>
<input id="Payment" size="7" type="text" value="0" /> $</div><br>
</div><br>
</form>


<br><br><h4>Facteur de multiplication selon le mois de l'ann&eacute;e.</h4><br>
Janvier: 30.8285<br>
F&eacute;vrier: 30.8075<br>
Mars: 30.8976<br>
Avril: 30.8765<br>
Mai: 30.8765<br>
Juin: 30.872<br>
Juillet: 30.8887<br>
Ao&#251;t: 30.8673<br>
Septembre: 30.8465<br>
Octobre: 30.8625<br>
Novembre: 30.8415<br>
D&eacute;cembre: 30.8575<br>
</body>
</html>


Again, thanks for your help!

xelawho
Mar 3rd, 2014, 03:00 PM
I don't know about the rest of the calculation, but here you are not passing in the value of f:


var pmt = PMT(i, n, -p);

whereas the PMT function takes 4 arguments:


function PMT(i, n, p, f) {


so f is undefined in that function and any calculation using f will come up NaN

so the first step would be


var pmt = PMT(i, n, -p, f);


(thanks for posting your full code, btw - made life a lot easier :thumbsup:)

Marc312
Mar 3rd, 2014, 03:23 PM
It worked! Thanks Xelawho!

xelawho
Mar 3rd, 2014, 03:26 PM
don't thank me - I have no idea what it's doing... rnd me did the heavy lifting :D