2012-12-11 130 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'); 
     } 
    } 
}