Internal Rate of Return Irr Calculator
function calculateIRR() {
var cashFlows = [];
// Initial outlay must be negative for the formula
var initial = parseFloat(document.getElementById('initialInvestment').value);
if (isNaN(initial) || initial === 0) {
alert("Please enter a valid initial investment (outlay).");
return;
}
cashFlows.push(-Math.abs(initial));
// Get year cash flows
for (var i = 1; i <= 6; i++) {
var val = parseFloat(document.getElementById('cf' + i).value);
if (!isNaN(val)) {
cashFlows.push(val);
}
}
if (cashFlows.length < 2) {
alert("Please enter at least one positive cash flow.");
return;
}
// Newton-Raphson Method
var guestRate = 0.1; // 10% initial guess
var maxIterations = 1000;
var precision = 0.0000001;
var resultRate = guestRate;
for (var j = 0; j < maxIterations; j++) {
var npv = 0;
var derivativeNpv = 0;
for (var t = 0; t 0) {
derivativeNpv -= t * cashFlows[t] / Math.pow(1 + resultRate, t + 1);
}
}
var newRate = resultRate – (npv / derivativeNpv);
if (Math.abs(newRate – resultRate) < precision) {
resultRate = newRate;
break;
}
resultRate = newRate;
}
var resultContainer = document.getElementById('irrResult');
var valueDiv = document.getElementById('resultValue');
var msgDiv = document.getElementById('resultMessage');
if (isNaN(resultRate) || !isFinite(resultRate)) {
resultContainer.style.display = "block";
resultContainer.style.backgroundColor = "#fdeaea";
valueDiv.innerText = "Error";
valueDiv.style.color = "#c0392b";
msgDiv.innerText = "Could not calculate IRR. Please check if your cash flows are realistic.";
} else {
resultContainer.style.display = "block";
resultContainer.style.backgroundColor = "#eafaf1";
valueDiv.innerText = (resultRate * 100).toFixed(2) + "%";
valueDiv.style.color = "#27ae60";
msgDiv.innerText = "Annualized Internal Rate of Return";
}
}