Public
Edited
Aug 10, 2023
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
r = apr / 100 / 12
Insert cell
t = years * 12
Insert cell
function getMonthly(P, r, t) {
return (P * (r * (1 + r) ** t)) / ((1 + r) ** t - 1);
}
Insert cell
monthly = getMonthly(P, r, t)
Insert cell
function reportValues(p_choice, t_choice, r_choice, monthly_choice) {
let local_p = p_choice;
let interests = [];
let principals_paid = [];
let principals_left = [];
for (let i = 0; i < t_choice; i++) {
let interest = local_p * r_choice;
interests.push(interest);
let principal = monthly_choice - interest;
principals_paid.push(principal);
principals_left.push(local_p - principal);
local_p -= principal;
}
return [interests, principals_paid, principals_left];
}
Insert cell
resultsNoRefi = reportValues(P, t, r, monthly)
Insert cell
Insert cell
resultsNoRefi[2].slice(-2)
Insert cell
all_interests = resultsNoRefi[0]
Insert cell
total_interests_no_refi = all_interests.reduce((a, b) => a + b, 0)
Insert cell
all_principals = resultsNoRefi[1]
Insert cell
nt = nyears * 12
Insert cell
pre_refi_interest = all_interests.slice(0, tBeforeRefi).reduce((a, b) => a + b, 0)
Insert cell
Insert cell
nP = P - all_principals.slice(0, tBeforeRefi).reduce((a, b) => a + b, 0)
Insert cell
nr = napr / 100 / 12
Insert cell
nmonthly = getMonthly(nP, nr, nt)
Insert cell
resultsRefi = reportValues(nP, nt, nr, nmonthly)
Insert cell
refi_interests = resultsRefi[0]
Insert cell
refi_principal = resultsRefi[1]
Insert cell
// sanity check, last number should either be verrrry small or 0 or negative very small, theese are js floating point processing issues
resultsRefi[2].slice(-2)
Insert cell
Insert cell
total_refi_interest = pre_refi_interest +
refi_interests.reduce((a, b) => a + b, 0)
Insert cell
Insert cell
amount_saved_in_interest = total_interests_no_refi - total_refi_interest
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more