JSFiddle - React, Tailwind, and code Playground
by Tan
HTML
<div id="test"></div>
JavaScript
// a = amount
// b = repayment (not rounded)
// c = term (in months)
var a = 60000;
var b = 450.35;
var c = 300;
// output function
document.getElementById('test').innerHTML = apr(a,b,c);
// apr function
function apr(a,b,c) {
var x=1.0001; var y1=0; var y2=0; var z=0;
do {
y1=b*(Math.pow(x,c+1)-x)/(x-1)-a;
y2=b*(c*Math.pow(x,c+1)-(c+1)*Math.pow(x,c)+1)/Math.pow(x-1,2)+c*Math.pow(x,c-1);
z=y1/y2;
x=x-z;
}
while (Math.abs(z)>1e-9);
var xyz=100*(Math.pow(1/x,12)-1);
return xyz.toFixed(1);
}