Javascript没有应用条件?

时间:2016-09-22 08:56:39

标签: javascript

编辑:我已将代码更新为新代码。一切都运行良好,除了一个月份搞乱的一行,就是这一行: if((q <1 || q> 28)&amp;&amp;(m == 2)&amp;&amp;(q%4 === 0)&amp;&amp;(!(q%100)))

所以我终于让我的计算器开始工作,但它并不遵守我的条件。如果我在9月40日输入例如它给了我一天。显然不应该发生。我知道哪里出错了?

由于

`function handleInput(form){         尝试{             var form = document.getElementById(“timeForm”);

setFile

2 个答案:

答案 0 :(得分:0)

这应该可以正常工作

function handleInput(form) {
    try {


            var form = document.getElementById("timeForm");

            var strYears = form.years.value;
            var strMonths = form.months.value;
            var strDays = form.days.value;
            var Y = parseInt(strYears);
            var m = parseInt(strMonths);
            var q = parseInt(strDays);
            var h = "";
            var output = "";

            if (isNaN(Y))
            throw ("Incorrect input. Years is not a number.");

            if (Y < 0 || Y > 9999)
            throw "Incorrect input. Years is out of expected range (0-9999).";

            if (isNaN(m))
            throw "Incorrect input. Months is not a number.";

            if (m < 1 || m > 12)
            throw "Incorrect input. Months is out of expected range (1-12).";

            if (isNaN(q))
            throw "Incorrect input. Days is not a number.";

            if ((q < 1 || q > 31) && (m == 1 || m==3 || m==5 || m==7 || m==8 || m==10 || m==12))
            throw "Incorrect input. Days is out of expected range (1-31).";

            if ((q < 1 || q > 30) && (m == 4 || m== 6 || m== 9 || m==11))
            throw "Incorrect input. Days is out of expected range (1-30).";

            if ((q < 1 || q > 28) && (m == 2)) 
            throw "Incorrect input. Days is out of expected range (1-28).";

            if (m == 1 || m == 2) {
                m = m + 12;
                Y = Y - 1;
                }

                h = (q + Math.floor(13 * (m + 1) / 5) + Y + Math.floor(Y / 4)
                - Math.floor(Y / 100) + Math.floor(Y / 400)) % 7;
                {

            if (h == 0)
                h = "Saturday";
            if (h == 1)
                h = "Sunday";
            if (h == 2)
                h = "Monday";
            if (h == 3)
                h = "Tuesday";
            if (h == 4)
                h = "Wednesday";
            if (h == 5)
                h = "Thursday";
            if (h == 6)
                h = "Friday";



            var output = h;
                document.getElementById("output").innerHTML = output;

                }
        }
        catch(error){
            document.getElementById("output").innerHTML = "Error: " + error;
                    }
    }

答案 1 :(得分:0)

有很多错误。我希望我能解决所有问题。

// Code goes here

function handleInput(form) {
  try {
    var form = document.getElementById("timeForm");

    var strYears = form.years.value;
    var strMonths = form.months.value;
    var strDays = form.days.value;

    var Y = parseInt(strYears);
    var m = parseInt(strMonths);
    var q = parseInt(strDays);
    var h = "";
    var output = "";

    if (isNaN(Y))
      throw ("Incorrect input. Years is not a number.");

    if (Y < 0 || Y > 9999)
      throw "Incorrect input. Years is out of expected range (0-9999).";

    if (isNaN(m))
      throw "Incorrect input. Months is not a number.";

    if (m < 1 || m > 12)
      throw "Incorrect input. Months is out of expected range (1-12).";

    if (isNaN(q))
      throw "Incorrect input. Days is not a number.";

    if ((q < 1 || q > 31) && ([1, 3, 5, 7, 8, 10, 12].indexOf(m) != -1))
      throw "Incorrect input. Days is out of expected range (1-31).";

    if ((q < 1 || q > 30) && ([4, 6, 9, 11].indexOf(m) != -1))
      throw "Incorrect input. Days is out of expected range (1-30).";

    if ((q < 1 || q > 28) && (m == 2))
      throw "Incorrect input. Days is out of expected range (1-28).";

    if (m == 1 || m == 2) {
      m = m + 12;
      Y = Y - 1;
    }

    h = (q + Math.floor(13 * (m + 1) / 5) + Y + Math.floor(Y / 4) - Math.floor(Y / 100) + Math.floor(Y / 400)) % 7; {

      if (h == 0)
        h = "Saturday";
      if (h == 1)
        h = "Sunday";
      if (h == 2)
        h = "Monday";
      if (h == 3)
        h = "Tuesday";
      if (h == 4)
        h = "Wednesday";
      if (h == 5)
        h = "Thursday";
      if (h == 6)
        h = "Friday";



      var output = h;
      document.getElementById("output").innerHTML = output;

    }
  } catch (error) {
    document.getElementById("output").innerHTML = "Error: " + error;
  }
}