否则部分if语句不执行

时间:2013-09-17 18:13:23

标签: javascript html

这个我一直在研究的JavaScript代码大部分都可以工作,但我一直在徘徊。正如你在评论中看到的那样,我在其他人身边工作,但是我无法将这部分靠近底部来执行。它是if(isCorrect!=“是”) 线。我把它作为if / else,但是else永远不会执行。所以我把它作为一个单独的if语句

function inputNum() {
    /*  initialize variables   */
    var inGuess = "";
    var spaceChar = " ";
    var loopCt;
    var guessResult = "";
    var correctNum;
    var correctNum = getRandomInt(1, 100);
    var guessNum = 0;
    var guessLeft = 10;
    var guessMsg = ""
    var isCorrect = "No";
    /*  added the following so that when they try the game again it resets the results */
    document.getElementById("fireworks").src = "images/Question_mark.png";
    resetTable();
    for (loopCt = 1; loopCt < 11; loopCt++) {
        inGuess = prompt("Please enter your guess(enter -1 to exit) Do not press Enter", "0");
        if (inGuess == "-1") {
            break;
        }
        if (inGuess == null || inGuess == "") {
            alert("Blanks are not allowed.  To exit enter '-1'.");
        } else {
            guessNum = parseInt(inGuess, 10);
            guessMsg = ", you have " + --guessLeft + " guesses left";
            if (inGuess == correctNum) {
                guessResult = "Correct!";
                isCorrect = "Yes";
                document.getElementById('emp' + loopCt).innerHTML = guessResult;
                document.getElementById('ct' + loopCt).innerHTML = inGuess;
                alert("Congratulations, you guess correctly!");
                break;
            } else if (guessNum < 1 || guessNum > 100) {
                alert("You must choose a number between 1 and 100");
            } else if (guessNum < correctNum) {
                guessResult = "Too low";
            } else {
                guessResult = "Too high";
            }
            document.getElementById('emp' + loopCt).innerHTML = guessResult + guessMsg;
            document.getElementById('ct' + loopCt).innerHTML = inGuess;
        }
    }
    if (isCorrect == "Yes") {
        document.getElementById("fireworks").src = "images/fireworks.jpg";
    }
    /*  for some reason the else is not being picked up so I have to change it to another if statement
else   
*/
    if (isCorrect != "Yes") {
        var errMsg = "The correct number was " + correctNum;
        errMsg = errMsg + "  Please try again.";
        document.getElementById(guessResult).innerHTML = errMsg;
        /* the line above this does not appear to function, don't know why so I am adding the line below*/
        document.getElementById("fireworks").src = "images/wrong.png";
        alert(errMsg);
    }
}
/*  random number function */
function getRandomInt(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
}
/* clear the results table */
function resetTable() {
    /*  the code below kept getting the error  TypeError: document.getElementById(...) is null  even though
I tried it with the code above that works so I have to do it brute force method
for (i = 1;i<11;i++)
{
        document.getElementById('emp'+i).innerHTML=" ";
        document.getElementById('ct'+i).innerHTML=" ";
}
*/
    document.getElementById('emp1').innerHTML = " ";
    document.getElementById('ct1').innerHTML = " ";
    document.getElementById('emp2').innerHTML = " ";
    document.getElementById('ct2').innerHTML = " ";
    document.getElementById('emp3').innerHTML = " ";
    document.getElementById('ct3').innerHTML = " ";
    document.getElementById('emp4').innerHTML = " ";
    document.getElementById('ct4').innerHTML = " ";
    document.getElementById('emp5').innerHTML = " ";
    document.getElementById('ct5').innerHTML = " ";
    document.getElementById('emp6').innerHTML = " ";
    document.getElementById('ct6').innerHTML = " ";
    document.getElementById('emp7').innerHTML = " ";
    document.getElementById('ct7').innerHTML = " ";
    document.getElementById('emp8').innerHTML = " ";
    document.getElementById('ct8').innerHTML = " ";
    document.getElementById('emp9').innerHTML = " ";
    document.getElementById('ct9').innerHTML = " ";
}

0 个答案:

没有答案