Discounted Cash Flow (DCF) Calculator
Discounted Cash Flow (DCF) Calculator
Net Present Value (NPV): $0.00
Total Discounted Cash Flow: $0.00
/* style.css */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Arial', sans-serif;
background-color: #f4f4f9;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.calculator-container {
background-color: #fff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
width: 100%;
max-width: 400px;
text-align: center;
}
h1 {
color: #333;
margin-bottom: 20px;
font-size: 24px;
}
.input-group {
margin-bottom: 15px;
}
label {
font-weight: bold;
display: block;
margin-bottom: 5px;
color: #555;
}
input {
width: 100%;
padding: 10px;
border-radius: 5px;
border: 1px solid #ddd;
font-size: 16px;
margin-bottom: 10px;
}
button {
background-color: #4CAF50;
color: white;
padding: 12px;
border: none;
border-radius: 5px;
width: 100%;
font-size: 16px;
cursor: pointer;
}
button:hover {
background-color: #45a049;
}
#result-container {
margin-top: 20px;
font-size: 18px;
color: #333;
}
#npv-value {
color: #4CAF50;
font-weight: bold;
}
#total-dcf {
color: #FF5722;
font-weight: bold;
}
@media (max-width: 600px) {
.calculator-container {
padding: 15px;
width: 90%;
}
}
// script.js
document.getElementById('dcf-form').addEventListener('submit', function(event) {
event.preventDefault();
const cashFlowsInput = document.getElementById('cash-flows').value;
const discountRate = parseFloat(document.getElementById('discount-rate').value);
const periods = parseInt(document.getElementById('periods').value);
if (!cashFlowsInput || !discountRate || !periods) {
alert("Please fill in all fields correctly!");
return;
}
// Parse the cash flows input into an array of numbers
const cashFlows = cashFlowsInput.split(',').map(val => parseFloat(val.trim()));
if (cashFlows.length !== periods) {
alert("The number of cash flows should match the number of periods.");
return;
}
// Calculate DCF
let npv = 0;
let totalDCF = 0;
const discountFactor = 1 + (discountRate / 100);
for (let i = 0; i < periods; i++) {
const discountedCashFlow = cashFlows[i] / Math.pow(discountFactor, i + 1);
npv += discountedCashFlow;
totalDCF += cashFlows[i];
}
// Display the result
document.getElementById('npv-value').textContent = `$${npv.toFixed(2)}`;
document.getElementById('total-dcf').textContent = `$${totalDCF.toFixed(2)}`;
});