Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Feb 2014
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Why does my script return a NaN error (Not a number)

    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:

    Code:
    <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:
    Code:
    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:

    Code:
    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.

  • #2
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,448
    Thanks
    11
    Thanked 598 Times in 578 Posts
    here's a type-safe version of PMT, which should fix your problem:

    Code:
    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.
    my site (updated 2014/10/20)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.3, IE11:9.2, IE9:2.7, IE10:2.6, FF:16.8, CH:47.5, SF:7.8, NON-MOUSE:37%

  • #3
    New to the CF scene
    Join Date
    Feb 2014
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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:

    Code:
    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:

    Code:
    <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!

  • #4
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,990
    Thanks
    56
    Thanked 557 Times in 554 Posts
    I don't know about the rest of the calculation, but here you are not passing in the value of f:
    Code:
    var pmt = PMT(i, n, -p);
    whereas the PMT function takes 4 arguments:
    Code:
    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
    Code:
    var pmt = PMT(i, n, -p, f);
    (thanks for posting your full code, btw - made life a lot easier )

  • Users who have thanked xelawho for this post:

    Marc312 (03-03-2014)

  • #5
    New to the CF scene
    Join Date
    Feb 2014
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts
    It worked! Thanks Xelawho!

  • #6
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,990
    Thanks
    56
    Thanked 557 Times in 554 Posts
    don't thank me - I have no idea what it's doing... rnd me did the heavy lifting


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •