Javascript计算函数不返回值

时间:2016-02-17 14:54:00

标签: javascript jquery html

附加的javascript函数应该使用两个输入范围中的值和表中初始值单元格的值。然后应该计算贷款的总还款额。

这并没有在&#tdd还款中输出。细胞如预期的那样?



< script >

  var repayment = function() {

  };

document.repaymentcalc.amount.onchange = repayment;

};

makeSomething();

function makeSomething() {
  $('tbody tr').each(function(idx, row) {
    var $row = $(row);
    var initialRateCell = $row.find('td')[1];
    var repaymentCell = $row.find('td')[3];
    var rate = parseFloat($(initialRateCell).html());
    var repaymentVal = computeRepayment(rate);
    $(repaymentCell).html(repaymentVal.repayment);
  });
}
$("#calcform :input").change(function() {
  makeSomething();
});

function computeRepayment(rate) {
  var x = parseInt(document.repaymentcalc.amount.value, 10);
  var y = parseInt(rate, 10) / 36500;
  var z = parseInt(document.repaymentcalc.numberpayments.value, 10);


var repayment = (x * y * z) + x;

return {
    repayment: '£' + repayment.toFixed(2),
};
}

< /script>
&#13;
</head>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>



<form name="repaymentcalc" id="calcform" action="">

  Amount
  <input id="amount" type="range" min="0" max="2500" step="50" value="100" style="width: 150px">Term
  <input id="numberpayments" type="range" min="0" max="420" step="30" value="30" style="width: 150px">


</form>

<table id="mortgagetable">

  <thead>

    <tr class="producthd">
      <th class="lenderhd">Lender</th>
      <th class="initialratehd">% p.a. fixed</th>
      <th class="rateshd">% APR</th>
      <th class="repaymenthd">Total Repayment</th>
    </tr>

  </thead>

  <tbody>

    <tr class="product">
      <td class="lender">Bank1</td>
      <td class="initialrate">285</td>
      <td class="rates">1204.39</td>
      <td class="repayment"></td>
    </tr>
    <tr class="product">
      <td class="lender">Bank2</td>
      <td class="initialrate">292</td>
      <td class="rates">1221.5</td>
      <td class="repayment"></td>
    </tr>
    <tr class="product">
      <td class="lender">Bank3</td>
      <td class="initialrate">292</td>
      <td class="rates">1295</td>
      <td class="repayment"></td>
    </tr>
    <tr class="product">
      <td class="lender">Bank4</td>
      <td class="initialrate">128</td>
      <td class="rates">1221.5</td>
      <td class="repayment"></td>
    </tr>

  </tbody>

</table>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:0)

function computeRepayment(rate) {
   var x = parseInt(document.repaymentcalc.amount.value, 10);
   var y = parseInt(rate, 10) / 36500;
   var z = parseInt(document.repaymentcalc.numberpayments.value, 10);


  return (x * y * z) + x;
}

并且您的功能还款看起来是空的

答案 1 :(得分:0)

您缺少函数中的return语句。如果要使用它,则需要返回值:

function computeRepayment(rate) {
  var x = parseInt(document.repaymentcalc.amount.value, 10);
  var y = parseInt(rate, 10) / 36500;
  var z = parseInt(document.repaymentcalc.numberpayments.value, 10);

  var repayment = (x * y * z) + x;

  return repayment // to return your value
}

您的代码中有一些错误。有一个空函数repayment和其他不正确的东西。在提出详细问题之前,请再次检查您的代码。

E.g:

第6行:删除};

答案 2 :(得分:0)

要实际从函数返回,您需要return一个值。

正如您在这个简单的例子中看到的那样:

function multiplyTwoNumbers(numberOne, numberTwo){
   var result = numberOne * numberTwo;
   return result; // Here you can see how the value is returned.
}
console.log(multiplyTwoNumbers(5,3); // Here we log the returned value.

在您的情况下,我认为您应该返回repayment变量。

function computeRepayment(rate) {
  var x = parseInt(document.repaymentcalc.amount.value, 10);
  var y = parseInt(rate, 10) / 36500;
  var z = parseInt(document.repaymentcalc.numberpayments.value, 10);

  var repayment = (x * y * z) + x;

  return repayment; // here you can see that your value is now returned
}