脚本不工作。 Lint说Semicolon需要

时间:2014-06-24 15:30:12

标签: javascript html datediff

我是Javascript编程的初学者,遇到了一个我无法解决的错误,因为我不了解代码。 Lint告诉我在行var dString = "September, 25, 2015";之前需要一个半冒号。我确实有这个工作,不知道我改变了什么。我从this问题获得了代码。



<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<html>

<head>
  <title>
    Saving
  </title>
</head>

<body>
  <script>
    function myFunction() {
      if (txtMoneySaved.value !== "" && !isNaN(txtMoneySaved.value) && txtMoneyGoal.value !== "" && !isNaN(txtMoneyGoal.value)) {
        // It's a number
        numValue = parseFloat(txtMoneySaved.value);
        DollarGoal = parseFloat(txtMoneyGoal.value);
        document.getElementById("demo").innerHTML = numValue;

        var DateDiff = {
          inDays: function(d1, d2) {
            var t2 = d2.getTime();
            var t1 = d1.getTime();
            return parseInt((t2 - t1) / (24 * 3600 * 1000), 10);
          },
          inWeeks: function(d1, d2) {
            var t2 = d2.getTime();
            var t1 = d1.getTime();
            return parseInt((t2 - t1) / (24 * 3600 * 1000 * 7), 10);
          },
          inMonths: function(d1, d2) {
            var d1Y = d1.getFullYear();
            var d2Y = d2.getFullYear();
            var d1M = d1.getMonth();
            var d2M = d2.getMonth();
            return (d2M + 12 * d2Y) - (d1M + 12 * d1Y);
          },
          inYears: function(d1, d2) {
            return d2.getFullYear() - d1.getFullYear();
          }
        }

        var dString = "September, 25, 2015";

        var d1 = new Date();
        var d2 = new Date(dString);

        var DolsDay = (DollarGoal - numValue) / DateDiff.inDays(d1, d2);
        var DolsWeek = (DollarGoal - numValue) / DateDiff.inWeeks(d1, d2);
        var DolsMonth = (DollarGoal - numValue) / DateDiff.inMonths(d1, d2);

        document.getElementById("DaysRemaining").innerHTML = "Number of <b>days</b> until " + dString + "<b>:</b> " + DateDiff.inDays(d1, d2) + "<br>Dollars per day to reach $8000<b>:</b> $" + DolsDay.ToFixed(2);
        document.getElementById("WeeksRemaining").innerHTML = "Number of <b>weeks</b> until " + dString + "<b>:</b> " + DateDiff.inWeeks(d1, d2) + "<br>Dollars per week to reach $8000<b>:</b> $" + DolsWeekToFixed(2);
        document.getElementById("MonthsRemaining").innerHTML = "Number of <b>months</b> until " + dString + "<b>:</b> " + DateDiff.inMonths(d1, d2) + "<br>Dollars per month to reach $8000<b>:</b> $" + DolsMonthToFixed(2);
      }
      //else some error message
    }
  </script>

  Enter the amount saved: <input type="text" name="Saved" id="txtMoneySaved" value="100"> Enter the goal amount:&nbsp;&nbsp;<input type="text" name="Goal" id="txtMoneyGoal" value="8000">

  <button onclick="myFunction()">Try it</button>

  <p id="demo"></p>

  <p id="DaysRemaining"></p>
  <p id="WeeksRemaining"></p>
  <p id="MonthsRemaining"></p>
  <p id="YearsRemaining"></p>

</body>

</html>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:1)

      ...inYears: function(d1, d2){
                    return d2.getFullYear()-d1.getFullYear();
                }
            };  //This should have the semicolon

            var dString = "September, 25, 2015";

答案 1 :(得分:0)

分号是可选的。忽略那个警告,这不是你的问题。

你有一些拼写错误:

  1. DolsDay.ToFixed(2)应为DolsDay.toFixed(2)。方法名称区分大小写
  2. 接下来的两行会忘记.之前的ToFixed,还应该使用小写t
  3. document.getElementById("DaysRemaining").innerHTML = "Number of <b>days</b> until "+dString+"<b>:</b> "+DateDiff.inDays(d1, d2)+"<br>Dollars per day to reach $8000<b>:</b> $"+DolsDay.toFixed(2);
    document.getElementById("WeeksRemaining").innerHTML = "Number of <b>weeks</b> until "+dString+"<b>:</b> "+DateDiff.inWeeks(d1, d2)+"<br>Dollars per week to reach $8000<b>:</b> $"+DolsWeek.toFixed(2);
    document.getElementById("MonthsRemaining").innerHTML = "Number of <b>months</b> until "+dString+"<b>:</b> "+DateDiff.inMonths(d1,d2)+"<br>Dollars per month to reach $8000<b>:</b> $"+DolsMonth.toFixed(2);
    

    修复示例,仍然没有(可选,但可读性很好)分号:http://codepen.io/paulroub/pen/Lbkno

答案 2 :(得分:0)

我将您的Javascript代码复制到here。它告诉我哪一行缺少分号。您可能希望将来使用此工具。

31                      }
32                  }; // missing semi-colon
33  
34                  var dString = "September, 25, 2015";
    ================^
    lint warning: missing semicolon
35  
36                  var d1 = new Date();

请注意,许多错误发生在指定的行号之前。在这种情况下,您在第32行后错过了分号。