2012-12-11 131 views
-1

可能有人請告訴我這是爲什麼不工作?我試着改變結構很多,但它似乎無所謂我放,當if else語句應用,它停止工作。除去if/else之間的分號 - 正如評論所說的函數與if語句不工作?

alert("y is equal to zero) 

加:

function wordSplit(){ 
     var sentence = document.getElementById("two").value; 
     var userWords=sentence.split(" "); 
     while(t<userWords.length){ 
      alert(userWords[t]); 
      t++ 
     }; 
     x = 0; 
     for (var x = 0; x < userWords.length; x++){ 
      y = 0; 
      for (var y = 0; y < vocab.length; y++){ 

       if (y<vocab.length) { 
        alert("y is less than vocab") 
       }; 
       else if (vocab[y] == userWords[x]){ 
        alert("y is equal to x") 
       }; 
       else if(y<vocab.length) { 
        alert("y is less than vocab 2") 
       }; 
       else if (y == vocab.length){ 
        alert(" y is equal to vocab length") 
       }; 
       else if (y == 0) 
       { 
        alert("y is equal to zero) 
       }; 

      }; 


     }; 
    }; 
+2

刪除分號。語法是'if(){} else {}'。 – JJJ

+0

if/else之間沒有分號。 – adeneo

+0

添加'「'零和) – David

回答

0

您不關閉您的最後警告的報價。

  if (y<vocab.length) { 
       alert("y is less than vocab") 
      } 
      else if (vocab[y] == userWords[x]){ 
       alert("y is equal to x") 
      } 
      else if(y<vocab.length) { 
       alert("y is less than vocab 2") 
      } 
      else if (y == vocab.length){ 
       alert(" y is equal to vocab length") 
      } 
      else if (y == 0) 
      { 
       alert("y is equal to zero") 
      } 
0

此:

  if (y<vocab.length) { 
       alert("y is less than vocab") 
      }; 
      else if (vocab[y] == userWords[x]){ 
       alert("y is equal to x") 
      }; 
      else if(y<vocab.length) { 
       alert("y is less than vocab 2") 
      }; 
      else if (y == vocab.length){ 
       alert(" y is equal to vocab length") 
      }; 
      else if (y == 0) 
      { 
       alert("y is equal to zero) 
      }; 

應該是這樣(注意分號和結束「):

  if (y<vocab.length) { 
       alert("y is less than vocab"); 
      } 
      else if (vocab[y] == userWords[x]){ 
       alert("y is equal to x"); 
      } 
      else if(y<vocab.length) { 
       alert("y is less than vocab 2"); 
      } 
      else if (y == vocab.length){ 
       alert(" y is equal to vocab length"); 
      } 
      else if (y == 0) 
      { 
       alert("y is equal to zero"); 
      } 

請注意,缺乏最終if else警報關閉"也,分號(;)不會在條件語句的走到底。

-1

以下是對代碼的快速編輯,其中包含預期結果的假設:

function wordSplit() { 

    var sentence = document.getElementById("two").value; 
    var userWords=sentence.split(" "); 
    var t = 0; 

    while(t < userWords.length) { 
     console.log(userWords[t]); 
     t++; 
    } 

    for (var x = 0; x < userWords.length; x++) { 
     var y = vocab.indexOf(userWords[x]); 
     if (y == -1) { 
     console.log(userWords[x] + ' is not found in vabulary list'); 
     } else { 
     console.log(userWords[x] + ' is found in vabulary list'); 
     } 
    } 
}