I'm trying to go through the problem sets from MITx 6.00.1x in JavaScript as a learning exercise to try to get a better handle on how to deal with algorithmic problem solving in JS rather than just working with frameworks/libraries and DOM manipulation like I have been for a while, but on the first assignment of the second problem set my values are consistently off after the second iteration of the for loop.
For reference, here are the correct values for the test case, where balance = 42, annual Interest Rate = 0.2 and the monthly payment rate = 0.04:
Month 1 Remaining balance: 40.99
Month 2 Remaining balance: 40.01
Month 3 Remaining balance: 39.05
Month 4 Remaining balance: 38.11
Month 5 Remaining balance: 37.2
Month 6 Remaining balance: 36.3
Month 7 Remaining balance: 35.43
Month 8 Remaining balance: 34.58
Month 9 Remaining balance: 33.75
Month 10 Remaining balance: 32.94
Month 11 Remaining balance: 32.15
Month 12 Remaining balance: 31.38
The values I'm getting from my code, though, are:
Month 1 Remaining balance: 40.992
Month 2 Remaining balance: 40.035
Month 3 Remaining balance: 39.101
Month 4 Remaining balance: 38.188
Month 5 Remaining balance: 37.297
Month 6 Remaining balance: 36.427
Month 7 Remaining balance: 35.577
Month 8 Remaining balance: 34.747
Month 9 Remaining balance: 33.936
Month 10 Remaining balance: 33.144
Month 11 Remaining balance: 32.371
Month 12 Remaining balance: 32.371
Here's my code, for reference.
//Function calculates the amount outstanding on a loan after one year of paying the exact minimum amount, assuming compound interest
function balanceAfterYear(balance, annualInterestRate, monthlyPaymentRate) {
//Rate at which interest builds monthly
var monthlyInterest = annualInterestRate / 12.0
//The minimum monthly payment, defined by the monthly payment rate (as a decimal) multiplied by the current outstanding balance
var minPayment = monthlyPaymentRate * balance;
//The unpaid balance for a given month is equal to the previous month's balance minus the minimum monthly payment
var unpaidBalance = balance - minPayment;
//the updated balance for a given month is equal to the unpaid balance + (the unpaid balance * the monthly interest rate). Initialized at 0 here because this does not apply during month 0 which is what the above values represent.
var updatedBalance = 0;
for (var i = 1; i < 12; i++) {
minPayment = monthlyPaymentRate * unpaidBalance;
updatedBalance = unpaidBalance + (unpaidBalance * monthlyInterest);
unpaidBalance = updatedBalance - minPayment;
}
return updatedBalance.toFixed(2);
}
Am I making a basic error in the logic that I'm just not spotting? Is it a rounding issue (i.e. would rounding values while doing the calculations rather than just at the end help)? Am I just missing something fundamental about JavaScript that I really should know by now?
I hope this doesn't get flagged as a repost, as I know many people have asked similar questions about this assignment in the past but almost certainly not in js.
Thank you for your time either way. Kind of feel like an idiot right now because I was able to do this in Python without any difficulties ages ago.