0

This JavaScript code I have been working on works for the most part but I keep running into quirks. I worked around the others as you can see in the comments, but I cannot get the part near the bottom to execute. It is the if (isCorrect != "Yes") line. I had it as an if/else, but the else would never execute. So I made it a separate if statement

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 = " ";
}
j08691
  • 190,436
  • 28
  • 232
  • 252
Clueless92
  • 31
  • 1
  • 3
    Are the only values for `isCorrect` "Yes" or "No"? If so, you really need to make this a boolean variable and use `true` and `false` instead. – David Sep 17 '13 at 18:18
  • I implore you to use K&R style coding in javascript. Why? http://stackoverflow.com/a/3218860/1497479. Thats why. – Jaime Torres Sep 17 '13 at 18:26
  • I have no idea how your else block would ever *not* be executed. You are checking `if (inGuess == correctNum)` which will never be true because `inGuess` is a string and `correctNum` a number. I think you meant to use `guessNum` there. – Ameen Sep 17 '13 at 18:32
  • changing to boolean did not change the outcome, it still does not execute. – Clueless92 Sep 17 '13 at 18:33
  • I am using notepad++ and it shows me my curly bracket pairing, but I will consider K&R. The inGuess == correctNum executes properly. – Clueless92 Sep 17 '13 at 18:35
  • I copied your code to fiddle and it looks like that your code works if you send the results to the console. Try it yourself here: http://jsfiddle.net/E7guj/ (open up the console view in your browser). Maybe your HTML structure doesn't match your javascript? That the value is not boolean has nothing to do with it. – Dr.Flink Sep 17 '13 at 18:36
  • but I see what you are referring to and I went back and changed inGuess to guessNum. but I am still having my original problem. – Clueless92 Sep 17 '13 at 18:38
  • I found the problem. It was erroring in the loop when no correct answers were found (loopct was too high), which is why it worked correctly on the true case. Thanks! – Clueless92 Sep 17 '13 at 18:49

0 Answers0