JSFiddle - React, Tailwind, and code Playground
by omarqa
HTML
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script src="http://knockoutjs.com/downloads/knockout-3.0.0.js"></script>
<div style="border: 2px; border-collapse: collapse; font-size: smaller;width: 180px">
<fieldset>
<legend>HBTF APR Calculator</legend>
Extra Costs:<input type="text" data-bind="value:extraCosts, valueUpdate: 'afterkeydown'"><br>
Loan Amount:<input type="text" data-bind="value:amount, valueUpdate: 'afterkeydown'"><br>
Interest Rate:<input type="text" data-bind="value:intresetRate, valueUpdate: 'afterkeydown'"><br>
Term (months):<input type="text" data-bind="value:terms, valueUpdate: 'afterkeydown'"><br>
<hr />
Payment:<input type="text" disabled="disabled" data-bind="value:payment"><br>
APR:<input type="text" disabled="disabled" data-bind="value:apr">
</fieldset>
</div>
JavaScript
$(function () {
function financecalc(dependent, values, decimals, stoeex, isBegin) {
var firstNPV;
var secondNPV;
var interpolationGuess;
var firstguess;
var secondguess;
var iteration;
if (values[0] < 0) {
return "Err n < 0";
}
if (values[1] <= -100) {
return "Err i < -100";
}
iteration = 1;
while (iteration <= 70) {
if (iteration == 1) {
firstguess = 0;
secondguess = 1;
} else {
firstguess = secondguess;
secondguess = interpolationGuess;
}
values[dependent] = firstguess;
if (values[1] <= -100)
break;
firstNPV = calcNPV(values, stoeex, isBegin, dependent == 0);
values[dependent] = secondguess;
if (values[1] <= -100)
break;
secondNPV = calcNPV(values, stoeex, isBegin, dependent == 0);
if (secondNPV > -0.00000001 && secondNPV < 0.00000001) {
if (dependent == 0)
if (secondguess - Math.floor(secondguess) > 0.01)
secondguess = Math.floor(secondguess) + 1;
else
secondguess = Math.floor(secondguess);
return ftostring(secondguess, decimals);
}
// if (Math.abs(secondguess - firstguess) < 0.00000001)
// break;
var interpolationB = (secondNPV - firstNPV) / (secondguess - firstguess); // B
interpolationGuess = firstNPV - firstguess * interpolationB; // A
interpolationGuess /= -interpolationB; // -A/B is the interpolation root
iteration++;
}
return "Unsolvable";
}
function calcNPV(values, stoeex, begin, isN) {
var n = values[0];
var i = values[1];
var pv = values[2];
...