Tap Forms – Organizer Database App for Mac, iPhone, and iPad › Forums › Script Talk › Javascript to calculate a monthly loan payment
Tagged: find monthly loan payment, JavaScript
- This topic has 0 replies, 1 voice, and was last updated 4 years, 3 months ago by Michael Tucker.
-
AuthorPosts
-
September 9, 2020 at 10:29 PM #41901
Michael TuckerParticipantI found this javascript example & tried to adapt it but seems I did not get it right.
I am trying to calculate a monthly payment into the field that is this script (shown below).
The number of years of the loan, the principal balance of the loan & the annual percentage rate are already inputted manually in the same form. The script references those data points & does math to find the monthly payment.
Perhaps it could be done without javascript with a calculation field but the calculation is not simple, and this existing javascript example (meant for a web form) seemed like it should be workable if only it were formatted for tapforms. A few parts included in the original example (commented out) I am not trying to get to work at this time, just the part that concerns the loan payment amount.
function calculatePayment() {
// Source: https://www.oreilly.com/library/view/javascript-the-definitive/0596000480/ch01s08.html
// Get the user’s input from the form. Assume it is all valid.
// Convert interest from a percentage to a decimal, and convert from
// an annual rate to a monthly rate. Convert payment period in years
// to the number of monthly payments.// var principal = document.loandata.principal.value;
// var interest = document.loandata.interest.value / 100 / 12;
// var payments = document.loandata.years.value * 12;var principal = record.getFieldValue(‘fld-a37b001914c9476e85ff6e72dc5f7e41’);
var interest = record.getFieldValue(‘fld-ad2e6a56376f462fa750fee0eca40902’) / 100 / 12;
var payments = record.getFieldValue(‘fld-3aaee77c789642dbb64633cac5b9b210’) * 12;// Now compute the monthly payment figure, using esoteric math.
var x = Math.pow(1 + interest, payments);
var monthly = (principal*x*interest)/(x-1);// Check that the result is a finite number. If so, display the results.
if (!isNaN(monthly) &&
(monthly != Number.POSITIVE_INFINITY) &&
(monthly != Number.NEGATIVE_INFINITY)) {x = round(monthly);
// document.loandata.total.value = round(monthly * payments);
// document.loandata.totalinterest.value = round((monthly * payments) – principal);
}
// Otherwise, the user’s input was probably invalid, so don’t
// display anything.else {
x = “”;
// document.loandata.total.value = “”;
// document.loandata.totalinterest.value = “”;
}
}// This simple method rounds a number to two decimal places.
function round(x) {
return Math.round(x*100)/100;
record.setFieldValue(scr_TP_MortgagePayment,round(x));
form.saveAllChanges();
}calculatePayment();
-
AuthorPosts
You must be logged in to reply to this topic.