View Single Post
Old 02-03-2013, 09:50 PM   PM User | #7
billboy
New Coder

 
Join Date: Feb 2013
Posts: 37
Thanks: 1
Thanked 0 Times in 0 Posts
billboy is an unknown quantity at this point
I am now getting an error "payments" is undefined

This is the code in a seperate js file


Code:
if (!Array.prototype.reduce) {
  Array.prototype.reduce = function reduce(accumulator){
    if (this===null || this===undefined) throw new TypeError("Object is null or undefined");
    var i = 0, l = this.length >> 0, curr;

    if(typeof accumulator !== "function") // ES5 : "If IsCallable(callbackfn) is false, throw a TypeError exception."
      throw new TypeError("First argument is not callable");

    if(arguments.length < 2) {
      if (l === 0) throw new TypeError("Array length is 0 and no second argument");
      curr = this[0];
      i = 1; // start accumulating at the second element
    }
    else
      curr = arguments[1];

    while (i < l) {
      if(i in this) curr = accumulator.call(undefined, curr, this[i], i, this);
      ++i;
    }

    return curr;
  };
}



function formatDollar(num) {
    var p = num.toFixed(2).split(".");
    return "$" + p[0].split("").reverse().reduce(function(acc, num, i, orig) {
        return  num + (i && !(i % 3) ? "," : "") + acc;
    }, "") + "." + p[1];
}



function centNotation(value)
 { 

 dollars = Math.floor(value); // chop floating point portion 
 cents = Math.floor((value % 1) * 100); // and chop everything else
 if (cents < 10) cents = "" + cents + "0";
 return(dollars + "." + cents);
 }


function validInt(value)
{
  

  for (var i=0; i < value.length; i++) {
    var c = value.charAt(i);
    if ((c < '0' || c > '9') && c != '.') {
      alert("There's a problem: all values herein must " +
        "be numbers only, and " + value + " has an unacceptable character " +
        "'" + c + "'");
      return(false);
    }
  }
  return(true);
}

function myPaymentCalc()
{
  
  
  var interestRate = document.debtcalc.interest.value,
      monthlyPayment = document.debtcalc.monthlypayment.value,
      principal = document.debtcalc.principal.value,
      accumulatingInterest = 1;
  
  if (! validInt(interestRate) || ! validInt(monthlyPayment) ||
      ! validInt(principal))
    return(false);
    
  if (interestRate == 0 || monthlyPayment == 0 || principal == 0) {
    alert("You need to specify your monthly payment, credit card " +
        "interest rate and current debt amount before I can calculate " +
        "how long it'll take for you to pay off your debt.");
    return(false);
  }
  
  interestRate = (interestRate > 1)? interestRate / 100.0 : interestRate;

  monthlyInterestRate = interestRate / 12;

  if (principal * monthlyInterestRate > monthlyPayment) {
    monthlyrate = "$" + centNotation(principal * monthlyInterestRate);
    permonthPayment = "$" + centNotation(monthlyPayment);
    alert("You've got a fundamental problem: your monthly payment of " +
        permonthPayment + " is less than the interest added each month " +
        "(which is " + monthlyrate + ") and you will never " +
        "pay off this debt. It might be a good time to call your creditors.");
    return(false);
  }
   
  interimVal1 = monthlyPayment - (monthlyInterestRate * principal)
  
  numberOfPayments = (Math.log(monthlyPayment) - Math.log(interimVal1)) /
        Math.log(1+monthlyInterestRate);
  
  numberOfPayments = Math.ceil(numberOfPayments);
      
  interimVal2 = Math.pow((monthlyInterestRate+1), (numberOfPayments-1));
  
  remaining = (principal * interimVal2) -
    (monthlyPayment/monthlyInterestRate) * (interimVal2 - 1);
    
  totalPayment = monthlyPayment *(numberOfPayments-1) + remaining;
        
  payments = numberOfPayments;
 totalpayment = "$" + centNotation(totalPayment);
 interestPaid = "$" + centNotation(totalPayment-principal);


 <!-- totalpayment = formatDollar(totalPayment);-->
 <!-- interestPaid = formatDollar(totalPayment-principal);-->


  
  
  alert("Given these figures, you'll end up having " + payments +
        " payments and pay a total of " + totalpayment + ", of which " +
        interestPaid + " is interest.");

document.debtcalc.numberofpayments.value = payments;
document.debtcalc.totalpayments.value = totalpayment; 
document.debtcalc.intpmt.value = interestPaid;


}
billboy is offline   Reply With Quote