1

Basically I want to count and display the amount of months it would take to get to a certain point (savings balance) based on a contribution every month.

Here is what I have so far:

function howLong(initial,interest,goal,added){
    var initialDeposit = parseInt(initial);
    var interestInt = parseInt(interest);
    var targetSaving = parseInt(goal);
    var contribution = parseInt(added);
    var monthCount = 0;
    while(initialDeposit <= targetSaving){
        monthCount++;
        initialDeposit+contribution
    }
    alert(monthCount)
}

Here is my html form:

<form>
Initial Deposit:<br />
<input type="number" id="initial"><br /><br />
Interest:<br />
<input type="number" id="interest"><br /><br />
Target savings amount:<br />
<input type="number" id="goal"><br /><br />
Monthly Contribution:<br />
<input type="number" id="contribution"><br /><br />
<input type="button" value="How Long!?" onclick="howLong(document.getElementById('initial').value,document.getElementById('interest').value,document.getElementById('goal').value),document.getElementById('contribution').value">
</form>
2
  • 3
    So what is the issue? Commented Nov 23, 2018 at 12:13
  • 2
    initialDeposit += contribution or initialDeposit = initialDeposit + contribution Commented Nov 23, 2018 at 12:13

4 Answers 4

1

You need to add the value of contribution to initialDeposit.

initialDeposit += contribution;

For the other problem, you have an error in the call of the function

document.getElementById('goal').value),document.getElementById('contribution').value"
                                     ^ >>>>>>>>>>>>>>>> should go >>>>>>>>>>>>>>>>> ^

shold be

onclick="howLong(
    document.getElementById('initial').value,
    document.getElementById('interest').value,
    document.getElementById('goal').value,
    document.getElementById('contribution').value
)"

The last round bracket is closing to early.

function howLong(initial, interest, goal, added) {
  var initialDeposit = parseInt(initial);
  var interestInt = parseInt(interest);
  var targetSaving = parseInt(goal);
  var contribution = parseInt(added);
  var monthCount = 0;
  while (initialDeposit <= targetSaving) {
    monthCount++;
    initialDeposit += contribution;
  }
  alert(monthCount)
}
<form>
  Initial Deposit:<br />
  <input type="number" id="initial"><br /><br /> Interest:
  <br />
  <input type="number" id="interest"><br /><br /> Target savings amount:<br />
  <input type="number" id="goal"><br /><br /> Monthly Contribution:<br />
  <input type="number" id="contribution"><br /><br />
  <input type="button" value="How Long!?" onclick="howLong(document.getElementById('initial').value,document.getElementById('interest').value,document.getElementById('goal').value,document.getElementById('contribution').value)">
</form>

Sign up to request clarification or add additional context in comments.

5 Comments

It must be a problem in my form, this just gives me monthCount = 1 and shows it in an alert (using values initialDeposit = 1000, targetSaving = 1500, contribution = 50).
@Kyle: it says 11 here: jsfiddle.net/jgf0xh7o. If you get rid of the loop (like this, you will get 10, which is actually the correct answer (you should have < in your condition, not <=).
@Groo Oh i see, so it's definitely my form then I guess?
right. it is the function, which have the closing bracket to early and omit the added parameter.
@Kyle: just note that <= is wrong, and you don't need a loop at all.
0

Change to initialDeposit += contribution;

Comments

0

As other answers have pointed out initialDeposit += contribution will solve the issue, but you don't need a loop here at all:

var monthCount = Math.ceil((targetSaving - initialDeposit)/contribution);

This is presuming that the contribution is constant, of course.

Comments

0

You were missing += operator and interest as well.

function howLong(){
    var initialDeposit = parseInt(document.getElementById('initial').value);
    var interestInt = parseInt(document.getElementById('interest').value);
    var targetSaving = parseInt(document.getElementById('goal').value);
    var contribution = parseInt(document.getElementById('contribution').value);
    var monthCount = 0;
    while(initialDeposit <= targetSaving){
        monthCount++;
        initialDeposit += contribution + interestInt; //+= was missing and interestInt as well
    }
    alert(monthCount);
}
<form>
Initial Deposit:<br />
<input type="number" id="initial"><br /><br />
Interest:<br />
<input type="number" id="interest"><br /><br />
Target savings amount:<br />
<input type="number" id="goal"><br /><br />
Monthly Contribution:<br />
<input type="number" id="contribution"><br /><br />
<input type="button" value="How Long!?" onclick="howLong()">
</form>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.