Results
Enter the initial investment and a series of expected cash flows to calculate the IRR.
function calculateIRR() {
var initialInvestment = parseFloat(document.getElementById("initialInvestment").value);
var cashFlowsText = document.getElementById("cashFlows").value;
var resultDiv = document.getElementById("result");
if (isNaN(initialInvestment) || cashFlowsText === "") {
resultDiv.innerHTML = "Please enter a valid initial investment and a list of cash flows.";
return;
}
var cashFlowsArray = cashFlowsText.split(',').map(function(item) {
return parseFloat(item.trim());
});
if (cashFlowsArray.some(isNaN)) {
resultDiv.innerHTML = "One or more cash flows are not valid numbers.";
return;
}
// The IRR calculation is an iterative process and cannot be solved directly with a simple formula.
// We need to use a numerical method to find the discount rate where NPV is zero.
// A common approach is the Newton-Raphson method or a bisection method.
// Let's implement a simple bisection method for approximation.
// We'll search for the IRR between a reasonable range, say -100% to 1000%.
var irrGuessLow = -0.99; // Represents -99%
var irrGuessHigh = 10.00; // Represents 1000%
var maxIterations = 100;
var tolerance = 0.0001;
var irr = null;
for (var i = 0; i = 0) {
// If not, we might need to adjust the search range or the function is ill-behaved
// For simplicity, we'll indicate failure or try a broader range if this happens often.
// In a real-world scenario, more robust error handling and range adjustment would be needed.
if (i === 0) { // First iteration, perhaps the range is too small or the IRR is outside.
// Let's try expanding the range slightly if the first attempt fails.
// This is a simplified approach.
irrGuessLow = -0.999;
irrGuessHigh = 20.0; // Wider range
continue;
} else {
resultDiv.innerHTML = "Could not find a suitable IRR within the search range. Try adjusting cash flows or consider if the investment is profitable.";
return;
}
}
// Calculate the midpoint
var irrGuessMid = (irrGuessLow + irrGuessHigh) / 2;
var npvMid = calculateNPV(irrGuessMid, initialInvestment, cashFlowsArray);
// Check for convergence
if (Math.abs(npvMid) < tolerance) {
irr = irrGuessMid;
break;
}
// Narrow down the search range
if (npvMid * npvLow < 0) {
irrGuessHigh = irrGuessMid;
} else {
irrGuessLow = irrGuessMid;
}
}
if (irr !== null) {
var irrPercentage = (irr * 100).toFixed(2);
resultDiv.innerHTML = "The approximate Internal Rate of Return (IRR) is:
" + irrPercentage + "%";
resultDiv.innerHTML += "This means the project is expected to yield an annual return of approximately " + irrPercentage + "%.";
} else {
resultDiv.innerHTML = "Could not find a suitable IRR after maximum iterations. Try adjusting cash flows or consider if the investment is profitable.";
}
}
function calculateNPV(rate, initialInvestment, cashFlows) {
var npv = -initialInvestment; // Start with the initial investment as a negative cash flow
for (var i = 0; i < cashFlows.length; i++) {
npv += cashFlows[i] / Math.pow(1 + rate, i + 1);
}
return npv;
}
.calculator-wrapper {
font-family: sans-serif;
max-width: 800px;
margin: 20px auto;
padding: 20px;
border: 1px solid #e0e0e0;
border-radius: 8px;
background-color: #f9f9f9;
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
display: flex;
flex-wrap: wrap;
gap: 20px;
}
.calculator-form {
flex: 1;
min-width: 300px;
}
.calculator-result {
flex: 1;
min-width: 300px;
background-color: #ffffff;
padding: 15px;
border-radius: 5px;
border: 1px solid #dcdcdc;
}
.calculator-form h2, .calculator-result h3 {
color: #333;
margin-top: 0;
border-bottom: 1px solid #eee;
padding-bottom: 10px;
margin-bottom: 20px;
}
.form-group {
margin-bottom: 15px;
}
.form-group label {
display: block;
margin-bottom: 5px;
font-weight: bold;
color: #555;
}
.form-group input[type="number"],
.form-group input[type="text"] {
width: calc(100% – 20px);
padding: 10px;
border: 1px solid #ccc;
border-radius: 4px;
font-size: 1em;
}
.calculator-form button {
background-color: #4CAF50;
color: white;
padding: 10px 15px;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 1em;
transition: background-color 0.3s ease;
}
.calculator-form button:hover {
background-color: #45a049;
}
#result p {
line-height: 1.6;
color: #333;
}
#result strong {
font-size: 1.1em;
}