Calculate compound interest to find the total balance, interest and growth over time using a fixed initial investment. Choose how frequently interest is compounded.
export const take = [
{ type: "number", label: "Principal", prefix: "$" },
{ type: "number", label: "Term", suffix: "years" },
{ type: "number", label: "Interest rate", step: 0.1, suffix: "%" },
{
type: "dropdown",
label: "Compound",
options: [
{ label: "Annually", value: 1 },
{ label: "Quarterly", value: 4 },
{ label: "Monthly", value: 12 },
],
},
]
export const make = ([principal, term, rate, freq]) => {
rate /= 100
const formula = (t) => principal * Math.pow(1 + rate / freq, freq * t)
const balance = formula(term)
const format = new Intl.NumberFormat("en-US", {
style: "currency",
currency: "USD",
}).format
return [
{
type: "text",
inline: true,
label: "Interest",
value: format(balance - principal),
},
{ type: "text", inline: true, label: "Balance", value: format(balance) },
{
type: "table",
label: "Growth over time",
columns: ["Year", "Interest", "Balance"],
value: Array.from({ length: term }, (_, i) => {
const term = i + 1
const balance = formula(term)
return [term, format(balance - principal), format(balance)]
}),
},
]
}