0

When i add two fields, the code doesn't sum it but instead, it adds both what I inputted, heres my code

function calculate_loan() {
  var amountBorrowed = document.form.amountBorrowed.value;
  var interest = document.form.interest.value / 100;
  var payments = document.form.durationOfPayment.value;
  var monthInterest = document.form.interestPerMonth.value;
  var totalInt = document.form.totalinterest.value;


 var monthly = amountBorrowed * interest;
 var totalinterest = monthly * payments;
 var totalAmount = amountBorrowed + totalinterest;


  if (!isNaN(monthly) &&
    (monthly != Number.POSITIVE_INFINITY) &&
    (monthly != Number.NEGATIVE_INFINITY)) {


    document.form.total.value = round(totalAmount);
    document.form.totalinterest.value =
      round(monthly * payments);
          document.form.interestPerMonth.value =
      round(monthly);
  }

  else {
    document.form.amountOfPayement.value = "";
    document.form.total.value = "";
    document.form.totalinterest.value = "";
  }
}

function round(x) {
  return Math.round(x * 100) / 100;
}

for example Interest per month is : 720 Total interest: 2160 Amount Borrowed: 12000 the result is 120002160. I want the result to be 14,160 please help thanks

1 Answers1

1

You should parse the values as numbers

var amountBorrowed = Number(document.form.amountBorrowed.value);
var interest = Number(document.form.interest.value) / 100;
var payments = Number(document.form.durationOfPayment.value);
var monthInterest = Number(document.form.interestPerMonth.value);
var totalInt = Number(document.form.totalinterest.value);
Alon Eitan
  • 11,997
  • 8
  • 49
  • 58