JSFiddle - React, Tailwind, and code Playground
by veer_vin
HTML
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<!--
Finance JS Calculations: http://financejs.org/
-->
<p>
Finance Am value is: <span id="amValue"></span>
</p>
<p>
Compound Annual Growth Rate is: <span id="ciValue"></span>
</p>
JavaScript
//Finance.js
//For more information, visit http://financejs.org
//Copyright 2014 - 2015 Essam Al Joubori, MIT license
// Instantiate a Finance class
var Finance = function() {};
// Present Value (PV)
Finance.prototype.PV = function (rate, cf1, numOfPeriod) {
numOfPeriod = typeof numOfPeriod !== 'undefined' ? numOfPeriod : 1;
var rate = rate/100, pv;
pv = cf1 / Math.pow((1 + rate),numOfPeriod);
return Math.round(pv * 100) / 100;
};
// Future Value (FV)
Finance.prototype.FV = function (rate, cf0, numOfPeriod) {
var rate = rate/100, fv;
fv = cf0 * Math.pow((1 + rate), numOfPeriod);
return Math.round(fv * 100) / 100;
};
// Net Present Value (NPV)
Finance.prototype.NPV = function (rate) {
var rate = rate/100, npv = arguments[1];
for (var i = 2; i < arguments.length; i++) {
npv +=(arguments[i] / Math.pow((1 + rate), i - 1));
}
return Math.round(npv * 100) / 100;
};
// seekZero seeks the zero point of the function fn(x), accurate to within x \pm 0.01. fn(x) must be decreasing with x.
function seekZero(fn) {
var x = 1;
while (fn(x) > 0) {
x += 1;
}
while (fn(x) < 0) {
x -= 0.01
}
return x + 0.01;
}
// Internal Rate of Return (IRR)
Finance.prototype.IRR = function(cfs) {
var args = arguments;
var numberOfTries = 1;
// Cash flow values must contain at least one positive value and one negative value
var positive, negative;
Array.prototype.slice.call(args).forEach(function (value) {
if (value > 0) positive = true;
if (value < 0) negative = true;
})
if (!positive || !negative) throw new Error('IRR requires at least one positive value and one negative value');
function npv(rate) {
numberOfTries++;
if (numberOfTries > 1000) {
throw new Error('IRR can\'t find a result');
}
var rrate = (1 + rate/100);
var npv = args[0];
for (var i = 1; i < args.length; i++) {
npv += (args[i] / Math.pow(rrate, i));
}
return npv;
}
return Math.round(seekZero(npv) * 100) /...