Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save uqmessias/0f5e320f96d83eaa357b09d90353be16 to your computer and use it in GitHub Desktop.
Save uqmessias/0f5e320f96d83eaa357b09d90353be16 to your computer and use it in GitHub Desktop.
/**
* @param {number} num
* @returns {string}
*/
function formatMoney(num) {
return `R$ ${num
.toFixed(2)
.replace('.', ',')
.replace(/(\d)(?=(\d\d\d)+(?!\d))/g, '$1.')}`;
}
function formatInteresPercentage(num) {
return (num * 100).toFixed(2) + '%';
}
function deductIR(investimentReturn, totalDaysInvested) {
if (totalDaysInvested < 0) {
return investimentReturn;
}
const [_, __, interestPercentage] = [
[0, 180, 0.225],
[181, 360, 0.2],
[361, 720, 0.175],
[721, Infinity, 0.15],
].find(([min, max]) => min <= totalDaysInvested && totalDaysInvested <= max);
return investimentReturn * (1 - interestPercentage);
}
function leaveMoneyInvestedAndPayInstallments(
totalAmount,
totalInstallments,
annualInterestPercentage,
) {
const installmentAmount = totalAmount / totalInstallments;
let currentTotalAmount = totalAmount;
console.log(
`Investing ${formatMoney(totalAmount)} for ${
totalInstallments - 1
} months at ${formatInteresPercentage(annualInterestPercentage)}/year`,
);
for (
let currentInstallmentNumber = 1;
currentInstallmentNumber <= totalInstallments;
currentInstallmentNumber++
) {
const monthlyInterestPercentage = Math.pow(annualInterestPercentage + 1, 1 / 12) - 1;
const currentInvestimentReturn = currentTotalAmount * monthlyInterestPercentage;
const actualCurrentInvestimentReturn = deductIR(
currentInvestimentReturn,
currentInstallmentNumber * 30,
);
let text = `\n\n${currentInstallmentNumber}/${totalInstallments}\n${formatMoney(
currentTotalAmount,
)} - ${formatMoney(installmentAmount)} + ${formatMoney(
actualCurrentInvestimentReturn,
)} (${formatInteresPercentage(monthlyInterestPercentage)}/month)`;
currentTotalAmount -= installmentAmount;
currentTotalAmount += actualCurrentInvestimentReturn;
text += ` = ${formatMoney(currentTotalAmount)}`;
console.log(text);
}
return currentTotalAmount;
}
/**
* console.log(formatMoney(leaveMoneyInvestedAndPayInstallments(10000.0, 10, 0.105)));
*
* 1/10
* R$ 10.000,00 - R$ 1.000,00 + R$ 64,75 (0.84%/month) = R$ 9.064,75
*
*
* 2/10
* R$ 9.064,75 - R$ 1.000,00 + R$ 58,70 (0.84%/month) = R$ 8.123,45
*
*
* 3/10
* R$ 8.123,45 - R$ 1.000,00 + R$ 52,60 (0.84%/month) = R$ 7.176,05
*
*
* 4/10
* R$ 7.176,05 - R$ 1.000,00 + R$ 46,47 (0.84%/month) = R$ 6.222,52
*
*
* 5/10
* R$ 6.222,52 - R$ 1.000,00 + R$ 40,29 (0.84%/month) = R$ 5.262,81
*
*
* 6/10
* R$ 5.262,81 - R$ 1.000,00 + R$ 34,08 (0.84%/month) = R$ 4.296,89
*
*
* 7/10
* R$ 4.296,89 - R$ 1.000,00 + R$ 28,72 (0.84%/month) = R$ 3.325,61
*
*
* 8/10
* R$ 3.325,61 - R$ 1.000,00 + R$ 22,23 (0.84%/month) = R$ 2.347,84
*
*
* 9/10
* R$ 2.347,84 - R$ 1.000,00 + R$ 15,69 (0.84%/month) = R$ 1.363,53
*
*
* 10/10
* R$ 1.363,53 - R$ 1.000,00 + R$ 9,11 (0.84%/month) = R$ 372,64
* R$ 372,64
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment