Need help with custom script to calculate cumulative interest value

Hi,

I’ve added a child table on Project doctype and I want to calculate cumulative interest depending on the “total_amount” and “financial_cost_percentage” values in the field “cumulative_interest”.

I’ve written a custom script to calculate the total amount for the items in the child table and I’ve tried to find out how I can calculate the cumulative interest. I have found a formula,
a-(a/{[(1+r)^n]-1}/[r(1+r)^n]*n), where a is the total amount and n is the duration and r is the rate.

This code calculates the value, but I’m unable to implement it in my custom script.

<script>
function myFunction() {
  var i = Interest Rate [i.e: 5%]/100;
  var r = i/12;
  var n = 36;
  var a = 1+r;
  var b = Math.pow(a, n);
  var c = b-1;
  var d = r*b;
  var m = Total Amount [i.e: 100000];
  var e = m/(c/d);
  var t = e*n;
  var f = t-m;
  document.getElementById("demo").innerHTML = f; 
}
</script>

Can someone help me with the code?

Thanks in advance.