// Internal Rate of Return of a Cash Flow Stream 

function IRR(cashFlow){
	this.cashFlow = cashFlow;
}

// returns result
IRR.prototype.result = function(){
	var irr = 0;
	var npwT = 0;
	if (this.npw(irr) > 0) {
		while (irr < 1) {
			npwT = this.npw(irr);
			if (npwT < 0) return irr;
			irr = irr + 0.0001;
		} 
	}
	if (this.npw(irr) < 0) {
		while (irr > -1) {
			npwT = this.npw(irr);
			if (npwT > 0) return irr;
			irr = irr - 0.0001;
		} 
	}
	return -1;
}

IRR.prototype.npw = function (irr){
	var npw = 0;
	for (var m = 0; m < this.cashFlow.length; m++){
		if (this.cashFlow[m] != 0) npw += this.cashFlow[m] / Math.pow((1 + irr), m);
	}
	return npw;
}

// returns input values
IRR.prototype.inputData = function(){
	return this.cashFlow + " " + this.i;
}

// Copyright engineeringtoolbox.com