home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 November / pcwk_11_98a.iso / Wtestowe / Money99 / money99.1 / mortgage / logic.js < prev    next >
Text File  |  1998-07-25  |  4KB  |  123 lines

  1. function min(x, y) {
  2.     if(x <= y)
  3.         return x;
  4.     else
  5.         return y;
  6. }
  7.  
  8. function max(x, y) {
  9.     if(x >= y)
  10.         return x;
  11.     else
  12.         return y;
  13. }
  14.  
  15. // -------------------------------
  16. // Calculate values for Mortgage 1
  17. // -------------------------------
  18. var rate1
  19. var loan1PaymentsPerYear
  20. var sto = parent;
  21.  
  22. if(sto.loan1Schedule == "annually")
  23.     loan1PaymentsPerYear = 1;
  24. else
  25. if(sto.loan1Schedule == "monthly")
  26.     loan1PaymentsPerYear = 12;
  27. else // loan1Schedule == "biweekly"
  28.     loan1PaymentsPerYear = 26;
  29.     
  30. // Periodic Interest rate = 1 + (user-entered rate / 100 / periods per year)
  31. rate1 = 1.0 + ( (sto.loan1Rate/100.0) / loan1PaymentsPerYear);
  32.  
  33. /*
  34. To calculate periodic payment amount, first sum the periodic interest rate over the
  35. total number of periods
  36.  
  37. set temp = 0
  38. for n = 0 to (total number of periods - 1)
  39.     temp += (periodic interest rate to the power of n)
  40.  
  41. Then the periodic payment amount can be calculated:
  42.  
  43. Periodic payment amount = (balloon payment * (periodic interest rate - 1))
  44. + ((initial loan amount - balloon payment) * (the periodic interest rate to
  45. the power of the total number of periods) / (interim value))
  46. */
  47.  
  48. for(var n=0, temptotal1 = 0; n < sto.loan1Length * loan1PaymentsPerYear; n++)
  49.     temptotal1 += Math.pow(rate1, n);
  50.  
  51. var payment1 = (sto.loan1Amount * Math.pow(rate1, (sto.loan1Length * loan1PaymentsPerYear))) /
  52.                 temptotal1;
  53.  
  54. var interestTotal1 = 0;
  55. var balance1 = sto.loan1Amount;
  56. var iYears1 = min(sto.yearsUntilSale, sto.loan1Length);
  57.  
  58. for(n = 0; n < iYears1 * loan1PaymentsPerYear; n++) {
  59.     var tmpInterest1 = balance1 * (rate1 - 1.0);
  60.     interestTotal1 += tmpInterest1;
  61.     balance1 = balance1 + tmpInterest1 - payment1;
  62. }
  63.  
  64. var pointTotal1 = Math.round(sto.loan1Fees + (sto.loan1Amount * ((sto.loan1Points / 100.0))));
  65. var costTotal1 = Math.round(sto.loan1Amount + pointTotal1 + interestTotal1);
  66. var interestTotal1 = Math.round(interestTotal1);
  67. var paymentsTotal1 = iYears1 * loan1PaymentsPerYear;
  68. payment1 = Math.round(payment1);
  69.  
  70. // -------------------------------
  71. // Calculate values for Mortgage 2
  72. // -------------------------------
  73. var rate2
  74. var loan2PaymentsPerYear
  75. var sto = parent;
  76.  
  77. if(sto.loan2Schedule == "annually")
  78.     loan2PaymentsPerYear = 1;
  79. else
  80. if(sto.loan2Schedule == "monthly")
  81.     loan2PaymentsPerYear = 12;
  82. else // loan2Schedule == "biweekly"
  83.     loan2PaymentsPerYear = 26;
  84.     
  85. // Periodic Interest rate = 1 + (user-entered rate / 100 / periods per year)
  86. rate2 = 1.0 + ( (sto.loan2Rate/100.0) / loan2PaymentsPerYear);
  87.  
  88. for(var n=0, temptotal2 = 0; n < sto.loan2Length * loan2PaymentsPerYear; n++)
  89.     temptotal2 += Math.pow(rate2, n);
  90.  
  91. var payment2 = (sto.loan2Amount * Math.pow(rate2, (sto.loan2Length * loan2PaymentsPerYear))) /
  92.                 temptotal2;
  93.  
  94. var interestTotal2 = 0;
  95. var balance2 = sto.loan2Amount;
  96. var iYears2 = min(sto.yearsUntilSale, sto.loan2Length);
  97.  
  98. for(n = 0; n < iYears2 * loan2PaymentsPerYear; n++) {
  99.     var tmpInterest2 = balance2 * (rate2 - 1.0);
  100.     interestTotal2 += tmpInterest2;
  101.     balance2 = balance2 + tmpInterest2 - payment2;
  102. }
  103.  
  104. var pointTotal2 = Math.round(sto.loan2Fees + (sto.loan2Amount * ((sto.loan2Points / 100.0))));
  105. var costTotal2 = Math.round(sto.loan2Amount + pointTotal2 + interestTotal2);
  106. var interestTotal2 = Math.round(interestTotal2);
  107. var paymentsTotal2 = iYears2 * loan2PaymentsPerYear;
  108. payment2 = Math.round(payment2);
  109.  
  110. // Create summary statement detailing which mortgage will cost more
  111.  
  112. difference = costTotal1 - costTotal2;
  113. if(costTotal1 == costTotal2)
  114.     strSummary = "After " + sto.yearsUntilSale + " years, <b>Mortgage 1 will cost you "
  115.         + "the same</b> amount as Mortgage 2.";
  116. else if(costTotal2 > costTotal1)
  117.     strSummary = "After " + sto.yearsUntilSale + " years, <b>Mortgage 1 will cost you "
  118.         + intToDollarStr(Math.abs(difference)) + " less</b> than Mortgage 2.";
  119. else // costTotal1 > costTotal2
  120.     strSummary = "After " + sto.yearsUntilSale + " years, <b>Mortgage 2 will cost you "
  121.         + intToDollarStr(Math.abs(difference)) + " less</b> than Mortgage 1.";
  122.  
  123.