-1

有人可以告诉我为什么这不起作用吗?我已经尝试了很多改变结构,但我放什么似乎并不重要,当应用 if else 语句时,它停止工作。

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

            };


        };
    };
4

3 回答 3

0

您没有关闭上次警报中的引号:

alert("y is equal to zero)

另外正如评论所说 - 删除if/else.

            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")
            }
于 2012-12-11T08:28:40.607 回答
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警报没有关闭。此外,分号 ( ;) 不会放在条件语句的末尾。

于 2012-12-11T08:30:16.300 回答
-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');
      }
    }
}
于 2012-12-11T08:35:13.510 回答