Loan Calculator JS not responding at all

Hi, I have no clue what is wrong with my code.

Console in DEV TOOLS is not popping out any error message.
Application is not working at all …

can you share with me some links or proper way how to process it?

thanks you a lot
Nancy

When you ask for help you should include the code you written as well as the challenge you are stuck on. Otherwise people here cant help you.

Hi Ronnehag,

i am sorry, I edited my post, it is link for codepen.io

1 Like

You are using a form, form default behavior is to refresh the page. For the submit eventListener you need to pass in event and preventDefault().

Also you add the click event to the document, not the submit button.

You need to set the value property on the inputs you are using as the output.
You can’t do totalPayment/12 that would be totalPayment.value/12.

Quick version, I didn’t touch the math

Summary
(() => {
  const submitButton = document.getElementById("submit");
  const form = document.getElementById("loan-form");

  const amountInput = document.getElementById("amount");
  const interestInput = document.getElementById("interest");
  const yearsInput = document.getElementById("years");

  const monthlyPaymentOutput = document.getElementById("monthly-payment");
  const totalPaymentOutput = document.getElementById("total-payment");
  const totalInterestOutput = document.getElementById("total-interest");

  submitButton.addEventListener("click", e => {
    // Prevent the form from submitting
    e.preventDefault();
	
    // Save the inputs, not needed but makes the calculations look cleaner
    const amount = amountInput.value;
    const interest = interestInput.value;
    const years = yearsInput.value;
	
    // Save the results
    const totalInterest = parseFloat(amount * interest).toFixed(2);
    const totalPayment = parseFloat(amount + totalInterest).toFixed(2);
    const monthlyPayment = parseFloat(totalPayment / 12).toFixed(2);
	
    // Set the values on the input elements that are used as outputs
    totalInterestOutput.value = totalInterest;
    totalPaymentOutput.value = totalPayment;
    monthlyPaymentOutput.value = monthlyPayment;
	
    // Reset the form
    form.reset();
  });
})();

thanks for help, the app is working at the moment

I guees it depends on the platform you use

In fact, I see no reason to use a loan calculator because an employee in a bank can calculate all expenses and interest in a minute and he will do it absolutely free. For example, my father for a long time could not calculate how much interest he should pay monthly, and so he decided to contact the bank where he planned to get a loan.