Javascript循环-在while条件中添加var?

时间:2018-11-23 12:11:22

标签: javascript loops while-loop

基本上,我想根据每月的供款来计算并显示达到特定点(储蓄余额)所需的月数。

这是我到目前为止所拥有的:

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)
}

这是我的html表单:

<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>

4 个答案:

答案 0 :(得分:1)

您需要将contribution的值添加到initialDeposit

initialDeposit += contribution;

对于另一个问题,您在调用该函数时遇到了错误

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

保持住

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

最后一个括号即将结束。

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>

答案 1 :(得分:0)

更改为initialDeposit += contribution;

答案 2 :(得分:0)

您也缺少+=运算符和兴趣。

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>

答案 3 :(得分:0)

正如其他答案所指出的那样,@RequestMapping(value="/createRunOne/saveRun", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE) public String saveRun(@RequestBody Run run, Model model){ try{ this.runService.saveRun(run); } catch (Exception ex){ model.addAttribute(ERROR, ex.getMessage()); ex.printStackTrace(); } return "success"; } 将解决问题,但是您根本不需要在此处循环:

success: function(response, data){
      if(data=='success'){
           window.location.replace('<redirect url>');
      }
},

当然,这是假定贡献是恒定的。