由于某些原因,Javascript函数无法使用某些if/else语句

由于某些原因,Javascript函数无法使用某些if/else语句,javascript,arrays,function,if-statement,Javascript,Arrays,Function,If Statement,出于某种奇怪的原因,我无法理解,当我在其中包含这些if/else语句时,代码将无法工作,当它们不存在时,代码可以正常工作。我假设这与我附加到if/else语句的条件有关,因为当使用更简单的条件时,它似乎是正确的。有人能告诉我我做错了什么吗 function wordSplit(){ var sentence = document.getElementById("two").value; var userWords=sentence.split(" ");

出于某种奇怪的原因,我无法理解,当我在其中包含这些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++
                };

            };


        };
    };
函数wordSplit(){
var语句=document.getElementById(“两个”).value;
var userWords=句子。拆分(“”);

虽然(t@DCoder是正确的,但是您需要删除额外的

HTML:

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++
            }

        }


    }
}​
wordSplit();
函数wordSplit(){
var vocab=[];
var语句=document.getElementById(“两个”).value;
var userWords=句子。拆分(“”);
var t=0;
while(t

之前丢失
;否则
。在浏览器的web开发工具中可以看到类似的小语法错误(通常通过按F12)。注意,第二个
if(y
(第三个
if
)是无用的。即使在修复此代码后,它也不会运行;导致它运行的条件已由第一个
if
处理。噢,我不知道F12快捷方式,谢谢提供信息。我将删除第三个if语句,谢谢:)谢谢!这很有效,所以;把一切都搞砸了吗?非常感谢你的更正:)
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++
            }

        }


    }
}​