Skip to content

Commit

Permalink
Enhanced calculator
Browse files Browse the repository at this point in the history
  • Loading branch information
smog-root committed Nov 3, 2024
1 parent 3634c35 commit c74877d
Showing 1 changed file with 47 additions and 29 deletions.
76 changes: 47 additions & 29 deletions calculators/calculators.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
// Counter animation
function animateValue(id, start, end, duration) {
// Utility function to get element value and parse it
function getValue(id, parseFunc = parseFloat) {
let value = document.getElementById(id).value;
return parseFunc(value) || 0;
}

// Counter animation with configurable currency symbol and duration
function animateValue(id, start, end, duration, currencySymbol = '₹') {
let range = end - start;
let current = start;
let increment = end > start ? 1 : -1;
Expand All @@ -8,7 +14,7 @@ function animateValue(id, start, end, duration) {

let timer = setInterval(function() {
current += increment;
obj.innerText = `${current.toLocaleString()}`;
obj.innerText = `${currencySymbol}${current.toLocaleString()}`;
if (current === end) {
clearInterval(timer);
}
Expand All @@ -17,39 +23,47 @@ function animateValue(id, start, end, duration) {

// EMI Calculator
function calculateEMI() {
let loanAmount = parseFloat(document.getElementById("loanAmount").value);
let interestRate = parseFloat(document.getElementById("interestRate").value) / 12 / 100;
let loanTenure = parseInt(document.getElementById("loanTenure").value);
let loanAmount = getValue("loanAmount");
let interestRate = getValue("interestRate") / 12 / 100;
let loanTenure = getValue("loanTenure", parseInt);

let emi = (loanAmount * interestRate * Math.pow(1 + interestRate, loanTenure)) /
(Math.pow(1 + interestRate, loanTenure) - 1);

animateValue("emiResult", 0, Math.round(emi), 1000);
if (loanAmount && interestRate && loanTenure) {
let emi = (loanAmount * interestRate * Math.pow(1 + interestRate, loanTenure)) /
(Math.pow(1 + interestRate, loanTenure) - 1);

animateValue("emiResult", 0, Math.round(emi), 1000);
} else {
alert("Please enter all required values for EMI calculation.");
}
}

// SIP Calculator with Chart
function calculateSIP() {
let monthlyInvestment = parseFloat(document.getElementById("monthlyInvestment").value);
let returnRate = parseFloat(document.getElementById("returnRate").value) / 12 / 100;
let investmentDuration = parseInt(document.getElementById("investmentDuration").value) * 12;
let monthlyInvestment = getValue("monthlyInvestment");
let returnRate = getValue("returnRate") / 12 / 100;
let investmentDuration = getValue("investmentDuration", parseInt) * 12;

let futureValue = monthlyInvestment * (Math.pow(1 + returnRate, investmentDuration) - 1) *
(1 + returnRate);
if (monthlyInvestment && returnRate && investmentDuration) {
let futureValue = monthlyInvestment * (Math.pow(1 + returnRate, investmentDuration) - 1) *
(1 + returnRate);

animateValue("sipResult", 0, Math.round(futureValue), 1000);
renderSIPChart(futureValue);
animateValue("sipResult", 0, Math.round(futureValue), 1000);
renderSIPChart(futureValue, investmentDuration);
} else {
alert("Please enter all required values for SIP calculation.");
}
}

// Render SIP Projection Chart
function renderSIPChart(futureValue) {
function renderSIPChart(futureValue, investmentDuration) {
const ctx = document.getElementById('sipChart').getContext('2d');
new Chart(ctx, {
type: 'line',
data: {
labels: Array.from({length: 10}, (_, i) => i + 1),
labels: Array.from({length: investmentDuration / 12}, (_, i) => i + 1),
datasets: [{
label: 'Projected Value',
data: Array.from({length: 10}, (_, i) => futureValue / 10 * (i + 1)),
data: Array.from({length: investmentDuration / 12}, (_, i) => futureValue / (investmentDuration / 12) * (i + 1)),
backgroundColor: 'rgba(75, 192, 192, 0.2)',
borderColor: 'rgba(75, 192, 192, 1)',
borderWidth: 1,
Expand All @@ -69,14 +83,18 @@ function renderSIPChart(futureValue) {

// Retirement Savings Calculator
function calculateRetirement() {
let currentAge = parseInt(document.getElementById("currentAge").value);
let retirementAge = parseInt(document.getElementById("retirementAge").value);
let monthlyExpenses = parseFloat(document.getElementById("monthlyExpenses").value);
let inflationRate = parseFloat(document.getElementById("inflationRate").value) / 100;
let currentAge = getValue("currentAge", parseInt);
let retirementAge = getValue("retirementAge", parseInt);
let monthlyExpenses = getValue("monthlyExpenses");
let inflationRate = getValue("inflationRate") / 100;

let yearsToRetirement = retirementAge - currentAge;
let futureMonthlyExpenses = monthlyExpenses * Math.pow(1 + inflationRate, yearsToRetirement);
let retirementCorpus = futureMonthlyExpenses * 12 * (80 - retirementAge);
if (currentAge && retirementAge && monthlyExpenses && inflationRate) {
let yearsToRetirement = retirementAge - currentAge;
let futureMonthlyExpenses = monthlyExpenses * Math.pow(1 + inflationRate, yearsToRetirement);
let retirementCorpus = futureMonthlyExpenses * 12 * (80 - retirementAge);

animateValue("retirementResult", 0, Math.round(retirementCorpus), 1000);
}
animateValue("retirementResult", 0, Math.round(retirementCorpus), 1000);
} else {
alert("Please enter all required values for retirement calculation.");
}
}

0 comments on commit c74877d

Please sign in to comment.