-1

I am trying to create goal calculator where user enter future required amount and know how much monthly investment required.

Ex: If i want 1200 Rs after 1 year how much monthly investment required with compound.

HTML file here

  <input type="number" id="goalAmt" placeholder="Goal Amount" required/>
  <input type="number" id="goalYear" placeholder="Year" required/>
  <input type="number" id="goanAnnualrate" placeholder="Annual Rate" required/>
  <button id="submitBtn" onClick="GoalCalculate();">Submit</button>

I have written below javascript.

function GoalCalculate() {

    var investment = parseInt(document.getElementById("goalAmt").value);
    var annualRate =
        parseInt(document.getElementById("goanAnnualrate").value);
    var monthlyRate = annualRate / 12 / 100;
    var years = parseInt(document.getElementById("goalYear").value);
    var months = years * 12;
    var futureValue = 0;
    var goalVal = 0;
    var currency = "INR";


    goalVal = investment * (1 + monthlyRate) / (months);

    document.getElementById("MI_req").innerHTML = currency + " "
        (~~goalVal);

}
  • 1
    Do you have a any test case? what to input and expected output – PEPEGA Sep 04 '19 at 11:10
  • Possible duplicate of [How to deal with floating point number precision in JavaScript?](https://stackoverflow.com/questions/1458633/how-to-deal-with-floating-point-number-precision-in-javascript) – AZ_ Sep 04 '19 at 11:12
  • 1
    what's your question? You forgot to describe any kind of problem or what specifically you need help with. What are you expecting the code to do, and what does it do instead? Please give a more precise description. Thanks. – ADyson Sep 04 '19 at 11:19

1 Answers1

1

If the calculation is right, you just forgot to add a + at the end where you show the result

function GoalCalculate() {
  var investment = parseInt(document.getElementById("goalAmt").value);
  var annualRate =
    parseInt(document.getElementById("goanAnnualrate").value);
  var monthlyRate = annualRate / 12 / 100;
  var years = parseInt(document.getElementById("goalYear").value);
  var months = years * 12;
  var futureValue = 0;
  var goalVal = 0;
  var currency = "INR";

  goalVal = investment * (1 + monthlyRate) / (months);
  document.getElementById("MI_req").innerHTML = currency + " " + (~~goalVal);
}
<input type="number" id="goalAmt" placeholder="Goal Amount" required/>
<input type="number" id="goalYear" placeholder="Year" required/>
<input type="number" id="goanAnnualrate" placeholder="Annual Rate" required/>
<button id="submitBtn" onClick="GoalCalculate()">Submit</button>
<h1 id="MI_req"></h1>
PEPEGA
  • 2,214
  • 20
  • 37