Skip to content

Commit

Permalink
Add CI
Browse files Browse the repository at this point in the history
  • Loading branch information
ebradyjobory committed Dec 21, 2014
1 parent 99627df commit c87e1d3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
18 changes: 12 additions & 6 deletions finance.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//Finance.js
//For more information, visit http://financejs.org.
//Created by Essam Al Joubori
//Protected under MIT license
//Copyright 2014 Essam Al Joubori, MIT license

// creating a Finance class
var Finance = function() {};
Expand Down Expand Up @@ -44,7 +44,6 @@ Finance.prototype.IRR = function(cfs) {
currentNPV +=(arguments[i] / Math.pow((1 + rate), i - 1));
}
//currentNPV = Finance.prototype.NPV(rate, arguments[0], arguments[1], arguments[2], arguments[3]);
console.log(rate)
if (currentNPV <= 0) {
bestGuess = rate;
return;
Expand Down Expand Up @@ -86,22 +85,22 @@ Finance.prototype.ROI = function(cf0, earnings) {
};

// Amortization
Finance.prototype.AM = function (principal, rate, period, yearOrMonth) {
Finance.prototype.AM = function (principle, rate, period, yearOrMonth) {
var ratePerPeriod = rate / 12 / 100;
// for inputs in years
if (!yearOrMonth) {
var numerator = ratePerPeriod * Math.pow((1 + ratePerPeriod), period * 12);
var denominator = Math.pow((1 + ratePerPeriod), period * 12) - 1;

var am = principal * (numerator / denominator);
var am = principle * (numerator / denominator);
return Math.round(am * 100) / 100;

// for inputs in months
} else if (yearOrMonth === 1) {
var numerator = ratePerPeriod * Math.pow((1 + ratePerPeriod), period);
var denominator = Math.pow((1 + ratePerPeriod), period) - 1;

var am = principal * (numerator / denominator);
var am = principle * (numerator / denominator);
return Math.round(am * 100) / 100;
} else {
console.log('not defined');
Expand Down Expand Up @@ -130,12 +129,19 @@ Finance.prototype.DF = function(rate, numOfPeriods) {
for (var i = 1; i < numOfPeriods; i++) {
discountFactor = 1 / Math.pow((1 + rate/100), (i - 1));
roundedDiscountFactor = Math.ceil(discountFactor * 1000)/1000;
console.log(roundedDiscountFactor);
dfs.push(roundedDiscountFactor);
}
return dfs;
};

// Compound Interest (CI)
Finance.prototype.CI = function(rate, numOfCompoundings, principle, numOfPeriods) {

var CI = principle * Math.pow((1 + ((rate/100)/ numOfCompoundings)), numOfCompoundings * numOfPeriods);

return Math.round(CI * 100) / 100;
};




7 changes: 6 additions & 1 deletion specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,14 @@ describe('FinanceJS', function() {
assert.equal(cal.PI(10, -40000, 18000, 12000, 10000, 9000, 6000), 1.09);
});

it('should compute DF', function() {
xit('should compute DF', function() {
// rate and number of periods
assert.equal(cal.DF(10, 6), [ 1, 0.91, 0.827, 0.752, 0.684]);
});

it('should compute CI', function() {
// rate, compoundings per period, principle , and number of periods
assert.equal(cal.CI(4.3, 4, 1500, 6 ), 1938.84);
});

});

0 comments on commit c87e1d3

Please sign in to comment.