0

由于一些我无法弄清楚的奇怪原因,当我在其中包含这些 if/else 语句时,代码将无法工作,当它们不存在时它可以正常工作。我认为这与我附加到 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) {
                    y++
                };
                else if (vocab[y] == userWords[x]){
                    y = 0;
                    x++
                };
                else if(y<vocab.length) {
                    y++
                };
                else if (y == vocab.length){
                    y = 0;
                };
                else if (y == 0)
                {
                    vocab.push(userWords[x]);
                    x++
                };

            };


        };
    };

重申一下,据我所知,问题肯定出在 if else 部分,当它被删除或更改为更简单时,它突然起作用了。

4

1 回答 1

0

@DCoder 是正确的,您需要删除额外的;

HTML:

<input type="text" id="two" value="What the hell is wrong here??" />​

JavaScript:

wordSplit();

function wordSplit() {
    var vocab = [];
    var sentence = document.getElementById("two").value;
    var userWords = sentence.split(" ");
    var t = 0;
    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) {
                y++
            } else if (vocab[y] == userWords[x]) {
                y = 0;
                x++
            } else if (y < vocab.length) {
                y++
            } else if (y == vocab.length) {
                y = 0;
            } else if (y == 0) {
                vocab.push(userWords[x]);
                x++
            }

        }


    }
}​

固定现场演示

于 2012-12-09T06:33:37.513 回答