否则,如果,不等于陈述

时间:2015-04-05 20:02:00

标签: javascript for-loop comparison

所以我正在研究一个项目,在其中我多次调用一个名为letterpool的数组,其中包含字母表中的所有字母。每次调用一个字母时,它都会从列表中删除,如果用户再次尝试选择它,则会收到错误消息。但是我似乎无法得到消息。确切的代码是

}

}
    }
        else if (userGuess.match(letters) && userGuess.length === 1) {
    for (var i = 0; i < letterpool.length; i++) {
        if (userGuess !== letterpool){
        document.getElementById("feedback_box").innerHTML= "You allready guess that one there skippy!"
        }
    }
        }
}

}else {
   document.getElementById("feedback_box").innerHTML= 'please enter one letter, numbers need not apply .';
}

所有代码

//JavaScript document
var answer = ['h', 'a', 'n', 'g', 'm', 'a', 'n'];
var wrong = ['b', 'c', 'd', 'e', 'f', 'i', 'j', 'k', 'l', 'o', 'p', 'q', 'r',
    's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
];
var letters = /^[a-zA-Z]*$/;
var answerLetter = document.getElementsByClassName("answer_char");
var letterpool = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
    'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
];
var leftarm = false
var rightarm = false
var head = false
var bodys = false
var leg = false
var rightleg = false

onload = init;

function init() {
    updateDisplay()

    document.getElementById("guess_button").onclick = function enter() {
        var userGuess = document.getElementById("guess_text").value;
        // This uses String.prototype.match() used too see if its both a letter and a single entry
        if (userGuess.match(letters) && userGuess.length === 1) {
            for (var i = 0; i < answer.length; i++) {
                if (userGuess === answer[i])
                // answer[i] correlates to answerLetter[i].
                    answerLetter[i].innerHTML = userGuess;
            }
            if (userGuess.match(letters) && userGuess.length === 1) {
                for (var i = 0; i < wrong.length; i++) {
                    if (userGuess === wrong[i])
                        if (!leftarm) {
                            document.getElementsByTagName("img")[1].src =
                                "img/leftarm_on.png";
                            leftarm = true;
                        } else if (leftarm && !head) {
                        document.getElementsByTagName("img")[3].src =
                            "img/rightarm_on.png";
                        head = true
                    } else if (head && !bodys) {
                        document.getElementsByTagName("img")[0].src =
                            "img/head_on.png";
                        bodys = true
                    } else if (bodys && !leg) {
                        document.getElementsByTagName("img")[2].src =
                            "img/body_on.png";
                        leg = true
                    } else if (leg && !rightleg) {
                        document.getElementsByTagName("img")[5].src =
                            "img/rightleg_on.png";
                        rightleg = true

                    } else if (rightleg) {
                        document.getElementsByTagName("img")[4].src =
                            "img/leftleg_on.png";
                        document.getElementById("feedback_box").innerHTML =
                            'sorry you are out of guesses please refresh!';

                    }

                }
            }   else if (userGuess.match(letters) && userGuess.length === 1) {
    for (var i = 0; i < letterpool.length; i++) {
        if (userGuess !== letterpool){
        document.getElementById("feedback_box").innerHTML= "You allready guess that one there skippy!"
        }
    }
        }

        } else {
            document.getElementById("feedback_box").innerHTML =
                'please enter one letter, numbers need not apply .';
        }

        /*var parent = document.getElementsByTagName("p")[2]*/
        if (userGuess.match(letters) && userGuess.length === 1) {
            for (var i = 0; i < letterpool.length; i++) {
                if (userGuess === letterpool[i])
                // answer[i] correlates to answerLetter[i].
                    letterpool.splice(i, 1);
                /* parent.removeChild(letterpool[i])    */
                updateDisplay()

            }

        }

        if (userGuess.match(letters) && userGuess.length === 1) {
            for (var i = 0; i < wrong.length; i++) {
                if (userGuess === wrong[i])
                    wrong.splice(i, 1);


            }
        }


    }




}




/* var userGuess = document.getElementById("guess_text").value;
      letterpool.splice(i, 1);
      for(var i = letterpool.length - 1; i >= 0; i--) {
    if(letterpool[i] === userGuess) {
       array.splice(i, 1);
    }
}
 delete letterpool[i];   
    var userGuess = document.getElementById("guess_text").value;*/

// This uses String.prototype.match() used too see if its both a letter and a single entry


function updateDisplay() {
    document.getElementById("letter_pool").innerHTML = letterpool




}

0 个答案:

没有答案