My calculator is producing some strange results when I input certain values. I have added it to jsfiddle so you can take a quick look.
For example, when I enter 4000 as the principal, set the duration to 1 year, and choose a 1% interest rate, the calculator displays repetitive values for both the principal and interest. Additionally, it skips every other month. However, if I increase the interest rate to 20%, the calculation starts to display the correct results for both the principal and interest. It's quite puzzling.
I suspect the issue lies with the 'intr' variable, but I'm unsure how to resolve it.
function calculateTotal(){
var body = document.body;
var tbl = document.createElement('table');
tbl.setAttribute('id', 'results');
var tblBody = document.createElement('tbody');
var tndiv = document.getElementById('tdcontainer');
for (var j = 1; j < payments; j++){
var row = document.createElement('tr');
temp = round(principal);
intr = round((monthly * payments) - principal);
while(temp>0 && intr>0){
if(tndiv != null){
var cell = document.createElement('td');
var cell2 = document.createElement('td');
var cell3 = document.createElement('td');
var ndiv = round(temp);
var intRate = round((monthly * payments) - principal);
var monthlyNum = j;
cell.innerHTML = ndiv;
cell2.innerHTML = monthlyNum;
cell3.innerHTML = intRate;
row.appendChild(cell);
row.appendChild(cell2);
row.appendChild(cell3);
j++;
}
temp-=monthly;
intr-=monthly;
tblBody.appendChild(row);
}
tbl.appendChild(tblBody);
body.appendChild(tbl);
tbl.setAttribute("border", "1");
}
}