2016-09-22 56 views
1

編輯:我已經更新了代碼到新的。一切工作正常,除了一個行是搞亂了那個月,這是這一個: if((q < 1 || q> 28)& &(m == 2)& &(q%4 === 0) & &(!(q%100)))Javascript沒有應用條件?

因此,我終於讓我的計算器開始工作,但它並沒有遵守我的條件。例如,如果我在9月40日打字,它會給我一天的時間。顯然這不應該發生。任何想法我錯了?

由於

`函數handleInput(形式){ 嘗試{ 變種形式=的document.getElementById( 「賽馬」);

  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; 
        } 
    }` 
+0

爲什麼如果條件(Q < 1 && q > 31)&&(M = 1 || || 3 5) –

+0

您使用'='的代替'分配值m =='比較一個月的時候,這樣你所做的只是給'm'分配一個值。 –

+0

此代碼甚至無效:未捕獲的SyntaxError:意外的代幣) – Damien

回答

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

謝謝!你將=改爲==而將&&改爲||?還是其他的東西,我沒有注意到? – brodude

+0

不,這是我的變化 –

+0

請upvote答案,如果它是正確 –

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

謝謝,那個工程。你知道爲什麼在添加這個時候2月會出現混亂嗎?它在28點後繼續計數。其他月份仍然有效。如果((q < 1 || q > 28)&&(m == 2)&&(q%4 === 0)&&(!(q%100))) – brodude

+0

您應該使用Year來檢查它是否平分((q < 1 || q > 28 )&&(m == 2)&&(Y%4 === 0)) –