Javascript没有应用条件

Javascript not applying conditions?

本文关键字:条件 应用 Javascript      更新时间:2023-09-26

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

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

感谢

' function handleInput(form) {尝试{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) && ( q % 4 === 0 ) && (!( q % 100 )))
            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;
                    }
    }`

应该没问题

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

有很多错误。我希望我把它们都修好了。

// 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;
  }
}