A Javascript Algorithm to Include Actual Cost of Insurance

Here, the assumed interest rate is included, along with monthly cost of insurance. Typically, for cash value life insurance, the cost is hidden from the consumer. Instead of cost, typical policies show premium, which is different.


<html>
<body>
<pre>
<script>
function log(arg) {
  document.body.innerHTML = document.body.innerHTML + arg + '\n<br>';
}

function logClear() {
  document.body.innerHTML = '';
}

function dateGenerateMM(month) {
  if(month>9) {
    return ""+month;
  } else {
    return "0"+month;
  }
}

function dateParse(dateString) {
  var mdy = dateString.split('/')
  return new Date(mdy[2], mdy[0]-1, mdy[1]);
}

function dateCalculateYearsBetween(date1, date2) {
  return (date2-date1)/(1000*60*60*24*365);
}

function createTransaction(date, amount) {
  var object = new Object();
  object.date = dateParse(date);
  object.amount = amount;
  return object;
}

function calculateNetWorth(transactionArray, interestRate) {
  var total = 0;
  for (var i=0; i<transactionArray.length; i++) {
    var P = transactionArray[i].amount;
    var r = interestRate;
    var y = transactionArray[i].years;
    total = total + P*Math.pow((1+r), y);
  }
  return total;
}

function printEndOfYearValues(transactionArray, interestRate, startMonth, startYear, numberOfYears, monthlyPaymentByYear) {
  for(var j=0;j<numberOfYears;j++){
    var yyyy = startYear + j;
   
    // Setup Monthly Payments
    var monthlyPayment = monthlyPaymentByYear[yyyy];
    var numberOfPayments = 0;
    for(var i=startMonth; i<=12; i++){
      var mm = dateGenerateMM(i);
      transactionArray[transactionArray.length] = createTransaction(mm+"/01/"+yyyy, monthlyPayment);
      numberOfPayments++;
    }
    var total = numberOfPayments*monthlyPayment;
   
    // Modify all transactions to include new end date
    var endOfYear = dateParse("12/31/"+yyyy);
    for (var i=0; i<transactionArray.length; i++) {
      transactionArray[i].years = dateCalculateYearsBetween(transactionArray[i].date, endOfYear);
    }
  
    log('Year:'+yyyy+' EndWorth:'+calculateNetWorth(transactionArray, interestRate)+' AnnualPayment:'+total);
   
    // For next year
    startMonth = 1;
  }
}

// Setup Interest Rate
var interestRate = 0.09;

// Initial deposit
var paymentArray = [];
paymentArray[0] = createTransaction("08/19/2014", 35000.00);

// Start calculation from 09/2014 and run to 12/2028
var startMonth = 9;
var startYear = 2014;
var numberOfYears = 15;

var monthlyPaymentByYear = [];
monthlyPaymentByYear[2014] = -1.3452*455; 
monthlyPaymentByYear[2015] = -1.8550*455;
monthlyPaymentByYear[2016] = -2.0174*455; 
monthlyPaymentByYear[2017] = -2.2008*455; 
monthlyPaymentByYear[2018] = -2.4074*455; 
monthlyPaymentByYear[2019] = -2.6383*455; 
monthlyPaymentByYear[2020] = -2.8908*455; 
monthlyPaymentByYear[2021] = -3.1583*455; 
monthlyPaymentByYear[2022] = -3.4383*455;
monthlyPaymentByYear[2023] = -3.7283*455;
monthlyPaymentByYear[2024] = -4.0324*455;
monthlyPaymentByYear[2025] = -4.3624*455;
monthlyPaymentByYear[2026] = -4.7266*455;
monthlyPaymentByYear[2027] = -5.1358*455;
monthlyPaymentByYear[2028] = -5.5983*455;
printEndOfYearValues(paymentArray, 0.09, startMonth, startYear, numberOfYears, monthlyPaymentByYear);
</script>
</pre>
</body>
</html>

Output:


Year:2014 EndWorth:33632.98636750427 AnnualPayment:-2448.264
Year:2015 EndWorth:26044.853064950486 AnnualPayment:-10128.3
Year:2016 EndWorth:16850.698444102545 AnnualPayment:-11015.003999999999
Year:2017 EndWorth:5773.343973589454 AnnualPayment:-12016.368