nnsusa nnsusa - 1 year ago 107
HTML Question

How to create a javascript table and get a calculations from input as table data

I'm creating a table using Javascript. I have 2 input; a principle amount and an annual interest rate. How can I create a loop with an output into a table of 10 years with principle+compounded interest rate. I'm not allowed to use jquery.

**I'm trying to display results as a table of the increased amount based on the principle and interest (compound interest); Year 1 Year 2 Year 3 Year 4 Year 5 1100 1210 1331 1464 1610 (10 years in a table, first row are years 1 to 10, second row are the amounts, sorry I don't know how to add a picture here)

Here is what I have started so far for the form input;

<!DOCTYPE html>

<title>Savings Account Calculator</title>


<h1>Savings Account Calculator</h1>


<form name="savingdata">

<td>Principle Amount:</td>
<td><input type="text" id="principle" value="1000" /></td>

<td>Annual Interest (%):</td>
<td><input type="text" id="interest" value="10" /></td>

<input type="button" onClick="calculateBy()" Value="Calculate" />

Answer Source

You can setup an empty div and use a table or other HTML element and populate the results with that.

JSFiddle - https://jsfiddle.net/xjj7vook/

function calculateBy() {

  var principle = document.getElementById('principle').value;
  var interest = document.getElementById('interest').value;
  interest = 1 + (interest / 100); //convert to dec
  var tableHTML = "<table>";
  var total = principle;
  for (i = 1; i < 10; i++) {
    var res = principle * (Math.pow(interest, i));
    res = res.toFixed(2);
    interestgained = +res - +principle;
    interestgained = interestgained.toFixed(2);
    tableHTML = tableHTML + "<tr>Year " + i + " | Principle: " + principle + " USD | Total: " + res + " USD | Interest: " + interestgained + " USD</tr><br>";

  tableHTML = tableHTML + "</table>";
  document.getElementById("someDIV").innerHTML = tableHTML;

Formula is not correct, I don't know what you are trying to calculate. This one just shows you how much interest you would gain per years.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download