diff --git a/script.js b/script.js index b94b29d..9cd811f 100644 --- a/script.js +++ b/script.js @@ -80,17 +80,53 @@ function runProjection(useInflation) { roth = roth * growthFactor; realEstate = realEstate * growthFactor; - pretaxSpend = Math.min(pretaxSpend, pretax * (1 - taxRate)); - pretax -= pretaxSpend / (1 - taxRate); - pretax = Math.max(0, pretax); + var cascadeSpend = 0; - aftertaxSpend = Math.min(aftertaxSpend, aftertax); - aftertax -= aftertaxSpend; - aftertax = Math.max(0, aftertax); + var grossPreTaxSpend = pretaxSpend / (1 - taxRate); + if (grossPreTaxSpend > pretax) { + cascadeSpend += pretaxSpend - pretax * (1 - taxRate); + pretax = 0; + } else { + pretax -= grossPreTaxSpend; + pretax = Math.max(0, pretax); + } - rothSpend = Math.min(rothSpend, roth); - roth -= rothSpend; - roth = Math.max(0, roth); + if (aftertaxSpend > aftertax) { + cascadeSpend += aftertaxSpend - aftertax; + aftertax = 0; + } else { + aftertax -= aftertaxSpend; + aftertax = Math.max(0, aftertax); + } + + if (rothSpend > roth) { + cascadeSpend += rothSpend - roth; + roth = 0; + } else { + roth -= rothSpend; + roth = Math.max(0, roth); + } + + if (cascadeSpend > 0) { + var grossCascade = cascadeSpend / (1 - taxRate); + if (aftertax > 0) { + var aftertaxDraw = Math.min(cascadeSpend, aftertax); + aftertax -= aftertaxDraw; + aftertax = Math.max(0, aftertax); + cascadeSpend -= aftertaxDraw; + } + if (cascadeSpend > 0 && pretax > 0) { + var pretaxDraw = Math.min(cascadeSpend / (1 - taxRate), pretax); + pretax -= pretaxDraw; + pretax = Math.max(0, pretax); + cascadeSpend -= pretaxDraw * (1 - taxRate); + } + if (cascadeSpend > 0 && roth > 0) { + var rothDraw = Math.min(cascadeSpend, roth); + roth -= rothDraw; + roth = Math.max(0, roth); + } + } pretaxSpend *= inflationFactor; aftertaxSpend *= inflationFactor;