0

Everything works fine but when i try to re run the function it is stuck on the alert box what did i do wrong and can anyone explain why its happening.You can look at the comment in the code to see the area im getting this probem

var userChoice = prompt("Do you choose rock, paper or scissors?");
 var computerChoice = Math.random();
    if (computerChoice < 0.34) {
            computerChoice = "rock";
        } else if(computerChoice <= 0.67) {
            computerChoice = "paper";
        } else {
            computerChoice = "scissors";
        } console.log("Computer: " + computerChoice);
        var compare = function(choice1,choice2){
            if (choice1 === choice2){
                return "The result is a tie!";

           }else if (choice1 === "rock"){
             if(choice2 === "scissors"){
                 return("rock wins");
             }else{
                return("paper wins");   
             }

           }else if (choice1 === "paper"){
                if(choice2 === "rock"){
                    return("paper wins");
                }else{
                    return("scissors wins");
                }
           }else if(choice1 === "scissors"){
               if(choice2 === "rock"){
                   return("rock wins");
               }else{
                   return("scissors wins");
               }
           }else if (choice1 != "rock"&&"paper"&&"scissors"){
                    alert("not a viable input,please try again");
                    compare(userChoice,computerChoice);
    //calling the function here makes the alert box repeatedly pop up

           }

        };
        compare(userChoice,computerChoice);
  • 2
    `choice1 != "rock"&&"paper"&&"scissors"` is supposed to be `choice1 != "rock"&&choice1 != "paper"&&choice1 != "scissors"`. Quasi-duplicate of http://stackoverflow.com/questions/20002503/why-does-a-b-or-c-or-d-always-evaluate-to-true. – Sebastian Simon Oct 26 '15 at 02:34
  • nope this isnt the problem this part actually works fine the problem is calling back the function to restart the function – Chris.locke Oct 26 '15 at 02:36
  • 2
    You have a case of recursion where the input and output is identical every time, causing an infinite loop. What are you trying to achieve? – Griffith Oct 26 '15 at 02:46
  • im trying to make the function run again after a user has made a different choice from what is required so they can re enter the correct information – Chris.locke Oct 26 '15 at 02:59
  • Take a look at your console for errors. –  Oct 26 '15 at 03:30
  • As Griffith said, you have a loop here, where the funciton will compare the choices, and if those choices are not "rock", "paper" and "scissors", it will compare again. Problem is it will always compare the same choices, since user doesn't have a chance to re-enter a new choice, so alert keep poping up. You should let user enter new choice after compare failed. – TJL Oct 26 '15 at 03:31

1 Answers1

0

You can set a flag and put everything in a while loop.

var finished = false;

var compare = function (choice1, choice2) {
   if (choice1 === choice2) {
       finished = true;
       return "The result is a tie!";
   } else if (choice1 === "rock") {
       if (choice2 === "scissors") {
       finished = true;
       return ("rock wins");
    } else {
       finished = true;
       return ("paper wins");
    }

} else if (choice1 === "paper") {
    if (choice2 === "rock") {
        finished = true;
        return ("paper wins");
    } else {
        finished = true;
        return ("scissors wins");
    }
} else if (choice1 === "scissors") {
    if (choice2 === "rock") {
        finished = true;
        return ("rock wins");
    } else {
        finished = true;
        return ("scissors wins");
    }
} else if (choice1 != "rock" && "paper" && "scissors") {
    alert("not a viable input,please try again");
    finished = false;
    //compare(userChoice, computerChoice); This causes recursion. Not necessary.

}

};

while (!finished) {
   var userChoice = prompt("Do you choose rock, paper or scissors?");
   var computerChoice = Math.random();

if (computerChoice < 0.34) {
    computerChoice = "rock";
} else if (computerChoice <= 0.67) {
    computerChoice = "paper";
} else {
    computerChoice = "scissors";
}

console.log("Computer: " + computerChoice);

alert(compare(userChoice, computerChoice));
}

Take a look at this fiddle http://jsfiddle.net/7p94axhp/

atomSmasher
  • 1,465
  • 2
  • 15
  • 37