Jump to content

if statements + document.getElememtById()


brooke_theperson

Recommended Posts

So, I have a code that asks a question, and if the answer in correct, it says correct in the div. If incorrect, it says incorrect. For some reason it always says incorrect even if my answer is correct. Please help, is there anything wrong with this code?

 

//*If players 1 turn    if (player == "1"){                //*Asks question        var trivia = prompt(questions_obj.question);                //*If answer is corerct do stuff        if (trivia === questions_obj.answer){        document.getElementById('correctincorrect').innerHTML = "Correct";            $('#character1').animate({left: "+=75px"}, 500, function(){                    //*If player gets to finish they win                    if (player1total == "8"){                        var winner1 = alert("Player 1 is the winner!");                        $('#character1').remove();                        $('#character2').remove();                        $('#character3').remove();                        $('#player1score').remove();                        $('#player2score').remove();                        $('#player3score').remove();                        $('#player1header').remove();                        $('#player2header').remove();                        $('#player3header').remove();                        $('#player1board').remove();                        $('#player2board').remove();                        $('#player3board').remove();                     }              });                            //*Adds up players score            player1total = player1total + 1;            document.getElementById("player1score").innerHTML = player1total;        }    if (trivia === questions_obj.answer2){        document.getElementById('correctincorrect').innerHTML = "Correct";            $('#character1').animate({left: "+=75px"}, 500, function(){                    //*If player gets to finish they win                    if (player1total == "8"){                        document.getElementById('correctincorrect').innerHTML = "Player 1 is the winner!";                        $('#character1').remove();                        $('#character2').remove();                        $('#character3').remove();                        $('#player1score').remove();                        $('#player2score').remove();                        $('#player3score').remove();                        $('#player1header').remove();                        $('#player2header').remove();                        $('#player3header').remove();                        $('#player1board').remove();                        $('#player2board').remove();                        $('#player3board').remove();                     }              });                            //*Adds up players score            player1total = player1total + 1;            document.getElementById("player1score").innerHTML = player1total;        }    if (trivia != questions_obj.answer || trivia != questions_obj.answer2){        document.getElementById('correctincorrect').innerHTML = "Incorrect";    }}
Link to comment
Share on other sites

The logic here is wrong:

if (trivia != questions_obj.answer || trivia != questions_obj.answer2){

That should be an && operator.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...