0

I am seriously pulling my hair out over this. I am writing a compound interest calculator in HTML and JS. The calculations are completely off. I don't know where I went wrong, since I get no error message. My HTML code:

<label for="Start_amnt">Initial investment:</label>
<br>
<input type="number" id="Start_amnt" name="Start_amnt" STYLE="font-family: Verdana; font-weight: bold; font-size: 12px; background-color: #FEFEFE; width:12em">
<br>
<label for="Percentage">Interest rate:</label> 
<br> 
<input type="number" id="Percentage" name="Percentage" STYLE="font-family: Verdana; font-weight: bold; font-size: 12px; background-color: #FEFEFE; width:7em">%
<br>
<label>Time period</label>
        <br>
        <input placeholder="0" type="number" id="Period_Year" name="Period_Year" STYLE="font-family: Verdana; font-weight: bold; font-size: 12px; background-color: #FEFEFE; width:5em"> <span>Years</span> 
        <input placeholder="0" type="number" id="Period_Month" name="Period_Month" STYLE="font-family: Verdana; font-weight: bold; font-size: 12px; background-color: #FEFEFE; width:5em"> <span>Months</span> 
            <br>
<button type="submit" onclick="calculate(Start_amnt.value, Percentage.value, Period_Year.value*12 + Period_Month.value)"> Calculate </button>

My JS code

function calculate(P,r,months) {
        for(i=0; i< months; i++){
            cashBack = P * Math.pow( 1 + r/100, months)
            P = cashBack
            console.log(cashBack);
        }
    }
Mahnez
  • 3
  • 3

2 Answers2

2

You need to parse the values of the inputs into numbers for the calculations - all inputs return string representations of the number the value indicates. If you don't parse them then the values are really strings and you will get funky outcomes.

function calculate(P,r,months) {
   const parsedP = parseInt(P, 10);
   const parsedR = parseInt(r, 10);
   let parsedMonths = parseInt(months, 10);

   for(i=0; i< parsedMonths ; i++){
     let cashBack = parsedP * Math.pow( 1 + parsedR /100, parsedMonths );
     parsedMonths  = cashBack;
     console.log(cashBack);
   }
 }
gavgrif
  • 15,194
  • 2
  • 25
  • 27
  • That doesn't seem to work. I get an error "Uncaught TypeError: Assignment to constant variable at calculate.(The line "parsedMonths = cashBack") and at HTMLButtonElement.onclick in the line ` ` – Mahnez Apr 02 '21 at 07:17
  • sorry - change the parsedMonths const statements to let... my bad - that is occurring becuase you are re-assigning the parsedMonths value. - also just note that I simply parsed the values as my answer indicated - i did not check the nature of the equation - check @Don R solution below to see if that impacts it as well. – gavgrif Apr 02 '21 at 08:37
  • That almost crashed my computer. It started counting to infinity thousands of times... I don't know if it's my code's fault or the suggestion you gave me. (I did check @Don R's solution and it did not work.) – Mahnez Apr 02 '21 at 09:03
  • The code did not work. Even after the fix you provided. – Mahnez Apr 02 '21 at 10:47
0

You're raising 1 + r/100 to the power of months; all you really want to do is multiply P by 1 + r/100:

cashBack = P * (1 + r/100)

EDIT: If you want the monthly amount on an annual interest rate, just further divide the rate by 12:

cashBack = P * (1 + r/100/12)
Don R
  • 573
  • 4
  • 10
  • That didn't quite work. It still spits out gibberish, but different gibberish. – Mahnez Apr 02 '21 at 07:25
  • That's odd. What browser are you testing in? When I run my version in Firefox on an investment of 100, interest rate of 1 for 6 months the output is 101 102.01 103.0301 104.060401 105.10100501 106.1520150601 which is pretty much exactly what I expect. – Don R Apr 02 '21 at 15:25
  • I am running it on Chrome. The values you input did give that response, but with an initial investment of 100, interest rate of 10 for 12 months, the output is 110.00000000000001 121.00000000000003 133.10000000000005 146.41000000000008 161.0510000000001 177.15610000000012 194.87171000000015 214.3588810000002 235.79476910000022 259.37424601000026 285.3116706110003 313.84283767210036 – Mahnez Apr 02 '21 at 15:55
  • Wait, are you wondering about the fractional part of the first value? This explains it as well as anything I've ever come across: https://stackoverflow.com/questions/588004/is-floating-point-math-broken – Don R Apr 02 '21 at 18:01
  • The interest rate was supposed to be annual. – Mahnez Apr 02 '21 at 18:11
  • That's something you ought to have mentioned in your question. Here's a good explanation of how to calculate monthly compounding of an annual rate: https://www.calculatorsoup.com/calculators/financial/compound-interest-calculator.php – Don R Apr 02 '21 at 20:27