Javascript Loop - Adding a var to the while condition?












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>









share|improve this question




















  • 3




    So what is the issue?
    – Ankit Agarwal
    Nov 23 '18 at 12:13






  • 2




    initialDeposit += contribution or initialDeposit = initialDeposit + contribution
    – Groo
    Nov 23 '18 at 12:13


















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>









share|improve this question




















  • 3




    So what is the issue?
    – Ankit Agarwal
    Nov 23 '18 at 12:13






  • 2




    initialDeposit += contribution or initialDeposit = initialDeposit + contribution
    – Groo
    Nov 23 '18 at 12:13
















1












1








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>









share|improve this question















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>






javascript loops while-loop






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 12:26







Kyle

















asked Nov 23 '18 at 12:11









KyleKyle

83




83








  • 3




    So what is the issue?
    – Ankit Agarwal
    Nov 23 '18 at 12:13






  • 2




    initialDeposit += contribution or initialDeposit = initialDeposit + contribution
    – Groo
    Nov 23 '18 at 12:13
















  • 3




    So what is the issue?
    – Ankit Agarwal
    Nov 23 '18 at 12:13






  • 2




    initialDeposit += contribution or initialDeposit = initialDeposit + contribution
    – Groo
    Nov 23 '18 at 12:13










3




3




So what is the issue?
– Ankit Agarwal
Nov 23 '18 at 12:13




So what is the issue?
– Ankit Agarwal
Nov 23 '18 at 12:13




2




2




initialDeposit += contribution or initialDeposit = initialDeposit + contribution
– Groo
Nov 23 '18 at 12:13






initialDeposit += contribution or initialDeposit = initialDeposit + contribution
– Groo
Nov 23 '18 at 12:13














4 Answers
4






active

oldest

votes


















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>








share|improve this answer























  • 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
    Nov 23 '18 at 12:22










  • @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
    Nov 23 '18 at 12:24












  • @Groo Oh i see, so it's definitely my form then I guess?
    – Kyle
    Nov 23 '18 at 12:32










  • right. it is the function, which have the closing bracket to early and omit the added parameter.
    – Nina Scholz
    Nov 23 '18 at 12:33










  • @Kyle: just note that <= is wrong, and you don't need a loop at all.
    – Groo
    Nov 23 '18 at 19:35



















0














Change to initialDeposit += contribution;






share|improve this answer





























    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.






    share|improve this answer





























      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>








      share|improve this answer























        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53446500%2fjavascript-loop-adding-a-var-to-the-while-condition%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        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>








        share|improve this answer























        • 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
          Nov 23 '18 at 12:22










        • @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
          Nov 23 '18 at 12:24












        • @Groo Oh i see, so it's definitely my form then I guess?
          – Kyle
          Nov 23 '18 at 12:32










        • right. it is the function, which have the closing bracket to early and omit the added parameter.
          – Nina Scholz
          Nov 23 '18 at 12:33










        • @Kyle: just note that <= is wrong, and you don't need a loop at all.
          – Groo
          Nov 23 '18 at 19:35
















        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>








        share|improve this answer























        • 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
          Nov 23 '18 at 12:22










        • @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
          Nov 23 '18 at 12:24












        • @Groo Oh i see, so it's definitely my form then I guess?
          – Kyle
          Nov 23 '18 at 12:32










        • right. it is the function, which have the closing bracket to early and omit the added parameter.
          – Nina Scholz
          Nov 23 '18 at 12:33










        • @Kyle: just note that <= is wrong, and you don't need a loop at all.
          – Groo
          Nov 23 '18 at 19:35














        1












        1








        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>








        share|improve this answer














        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>








        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>





        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>






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 23 '18 at 12:33

























        answered Nov 23 '18 at 12:14









        Nina ScholzNina Scholz

        177k1491156




        177k1491156












        • 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
          Nov 23 '18 at 12:22










        • @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
          Nov 23 '18 at 12:24












        • @Groo Oh i see, so it's definitely my form then I guess?
          – Kyle
          Nov 23 '18 at 12:32










        • right. it is the function, which have the closing bracket to early and omit the added parameter.
          – Nina Scholz
          Nov 23 '18 at 12:33










        • @Kyle: just note that <= is wrong, and you don't need a loop at all.
          – Groo
          Nov 23 '18 at 19:35


















        • 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
          Nov 23 '18 at 12:22










        • @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
          Nov 23 '18 at 12:24












        • @Groo Oh i see, so it's definitely my form then I guess?
          – Kyle
          Nov 23 '18 at 12:32










        • right. it is the function, which have the closing bracket to early and omit the added parameter.
          – Nina Scholz
          Nov 23 '18 at 12:33










        • @Kyle: just note that <= is wrong, and you don't need a loop at all.
          – Groo
          Nov 23 '18 at 19:35
















        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
        Nov 23 '18 at 12:22




        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
        Nov 23 '18 at 12:22












        @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
        Nov 23 '18 at 12:24






        @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
        Nov 23 '18 at 12:24














        @Groo Oh i see, so it's definitely my form then I guess?
        – Kyle
        Nov 23 '18 at 12:32




        @Groo Oh i see, so it's definitely my form then I guess?
        – Kyle
        Nov 23 '18 at 12:32












        right. it is the function, which have the closing bracket to early and omit the added parameter.
        – Nina Scholz
        Nov 23 '18 at 12:33




        right. it is the function, which have the closing bracket to early and omit the added parameter.
        – Nina Scholz
        Nov 23 '18 at 12:33












        @Kyle: just note that <= is wrong, and you don't need a loop at all.
        – Groo
        Nov 23 '18 at 19:35




        @Kyle: just note that <= is wrong, and you don't need a loop at all.
        – Groo
        Nov 23 '18 at 19:35













        0














        Change to initialDeposit += contribution;






        share|improve this answer


























          0














          Change to initialDeposit += contribution;






          share|improve this answer
























            0












            0








            0






            Change to initialDeposit += contribution;






            share|improve this answer












            Change to initialDeposit += contribution;







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 23 '18 at 12:14









            Rafael de FreitasRafael de Freitas

            145




            145























                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.






                share|improve this answer


























                  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.






                  share|improve this answer
























                    0












                    0








                    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.






                    share|improve this answer












                    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.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 23 '18 at 12:18









                    GrooGroo

                    35.3k1484158




                    35.3k1484158























                        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>








                        share|improve this answer




























                          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>








                          share|improve this answer


























                            0












                            0








                            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>








                            share|improve this answer














                            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>








                            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>





                            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>






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Nov 23 '18 at 13:00

























                            answered Nov 23 '18 at 12:17









                            Prasad TelkikarPrasad Telkikar

                            1,862419




                            1,862419






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.





                                Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                Please pay close attention to the following guidance:


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53446500%2fjavascript-loop-adding-a-var-to-the-while-condition%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                What visual should I use to simply compare current year value vs last year in Power BI desktop

                                Alexandru Averescu

                                Trompette piccolo