Jump to content

loop in function that shouldn't loop


brooke_theperson

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!");        }    }}
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...