0

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.

  • if you just use debugger and step through the function, I am sure you can find the issue – Huangism Jun 01 '17 at 20:18
  • 3
    [is floating point math broken](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – adeneo Jun 01 '17 at 20:19

2 Answers2

0

It looks like you're not truncating your numbers when you need to. Your bank account will keep track of your money down to the cent - 2 decimal places. After paying interest, for example, your first month's balance remaining should be $40.99, not $40.992. Either truncate or round off to the cent. I think each subsequent month is adding onto the error that the 3rd decimal place introduces. You might want to see JavaScript math, round to two decimal places.

Arnav Aggarwal
  • 769
  • 5
  • 7
0

Your algorithm is wrong. All you need to do is deduct payment and accrue interest:

function f(b, r, m) {
  for (var i = 0; i < 12; i++) {
    b = b*(1-m)*(1+r/12);
  }
  return b.toFixed(2);
}

console.log(f(42, 0.2, 0.04));
Fabricator
  • 12,722
  • 2
  • 27
  • 40
  • Thank you very much, fundamental algorithmic errors like this in things that I have written correctly before are pretty embarrassing. I should have known better than to blame the language and post a question rather than consider the (much more likely) possibility that I was doing something wrong on a basic level. – TrypanosomaBruceii Jun 08 '17 at 17:51