Jump to content
Sign in to follow this  
brooke_theperson

loop in function that shouldn't loop

Recommended Posts

So I have this function that firsts asks the player, which player they are, then asks them a question. if they get it right their score increases by one, and an alert pops up that says correct. The problem is, after you answer a question, an alert pops up that says correct, and then another question pops up. No matter what your answer is nothing happens afterwards. Basically after everything happens, another question comes up but it shouldn't. Can anyone tell me if there is anything wrong with my code that might make this happen?

 

 function question(question, answer) {    this.question = question;    this.answer = answer;}var a = new question("What letter comes after 'a'?", "b")var b = new question("What letter comes after 'b'?", "c")var allquestions = [a, b];var player1total = 0;var player2total = 0;var player3total = 0;  function ask(){    var num = Math.floor(Math.random() * allquestions.length);    var questions_obj = allquestions[num];    var player = prompt("What player are you?" + "     (Hint: use your player number not your name.)");        if (player = "1"){        var trivia = prompt(questions_obj.question);        if (trivia === questions_obj.answer){            player1total = player1total + 1;            document.getElementById("player1score").innerHTML = player1total;            var correct = alert("That is correct!");            $("#character1").animate({top: "-=100px"}, 500);        }        else{            var incorrect = alert("That is incorrect!");        }    }    if (player = "2"){        var trivia = prompt(questions_obj.question);        if (trivia == questions_obj.answer){            player2total = player2total + 1;            document.getElementById("player2score").innerHTML = player2total;            var correct = alert("That is correct!");        }        else{            var incorrect = alert("That is incorrect!");        }    }    if (player = "3"){        var trivia = prompt(questions_obj.question);        if (trivia == questions_obj.answer){            player3total = player3total + 1;            document.getElementById("player3score").innerHTML = player3total;            var correct = alert("That is correct!");        }        else{            var incorrect = alert("That is incorrect!");        }    }}

Share this post


Link to post
Share on other sites

You have one equal sign in the player if check, should be ==

 

I'm not sure if that is the issue but upon first glance, I noticed.

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...