-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 に答える