Jump to content
Sign in to follow this  
brooke_theperson

if statements + document.getElememtById()

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";    }}

Share this post


Link to post
Share on other sites

The logic here is wrong:

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

That should be an && operator.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...