Skip to content

Instantly share code, notes, and snippets.

@shreshthmohan
Created April 28, 2021 08:40
Show Gist options
  • Save shreshthmohan/23208d251e8534e0c1aa744594bdb888 to your computer and use it in GitHub Desktop.
Save shreshthmohan/23208d251e8534e0c1aa744594bdb888 to your computer and use it in GitHub Desktop.
Code for calculating interest on a recurring deposit
// For now only works for number of months that are multiples of 3
// Compunding quarterly (every 3 months)
function rd(monthly_contribution, interest_rate, duration_years) {
const duration_months = Math.floor(duration_years * 12);
const months_compounding = 3;
let total = 0;
const quarterly_contribution =
monthly_contribution * (1 + (1 * interest_rate) / 1200) +
monthly_contribution * (1 + (2 * interest_rate) / 1200) +
monthly_contribution * (1 + (3 * interest_rate) / 1200);
for (let i = 1; i <= duration_months / 3; i++) {
total =
total +
quarterly_contribution *
(1 + interest_rate / 400) ** Math.floor(duration_months / 3 - i);
return {
total,
interest_earned:
total - monthly_contribution * Math.floor(duration_years * 12),
};
}
console.log(JSON.stringify(rd(1000, 4.9, 1)));
console.log(JSON.stringify(rd(1000, 5, 2)));
console.log(JSON.stringify(rd(1000, 5.35, 5)));
console.log(JSON.stringify(rd(1000, 5.5, 10)));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment