0

I was able to do it with the var computerChoice = Math.random(); … and turning that variable into rock, paper or scissors… but now I can't get the variable 'winner' to change so that it prints out the results. It just keeps printing out 'undefined'. Help?

 var choice1 = userChoice;
 var choice2 = computerChoice;


 var winner = "undefined";


 var compare = function (choice1, choice2) {//start function

     if (choice1 === choice2) {
         winner = "Tie game!";


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

}//end function


confirm("You chose: " + userChoice + "\nSystem: " + computerChoice + "\nOutcome: " + winner);


compare(userChoice,computerChoice);

</script>
2
  • 1
    You have to call compare before you call confirm. Commented May 9, 2016 at 20:51
  • please use a switch instead the ifs it will make your code look better easier to read Commented May 9, 2016 at 20:52

3 Answers 3

2

That's because you aren't changing it before you display it. Call compare before you call confirm since compare is the function that changes winner.

compare(userChoice, computerChoice);
confirm("You chose: " + userChoice + "\nSystem: " + computerChoice + "\nOutcome: " + winner);
Sign up to request clarification or add additional context in comments.

Comments

0

You should change the order of the function calls.

compare(userChoice,computerChoice);
confirm("You chose: " + userChoice + "\nSystem: " + computerChoice + "\nOutcome: " + winner);

Comments

0

You can put 'confirm' inside the 'compare' function block and delete all of these global variables

choice1 ,userChoice; choice2 , computerChoice;

     var compare = function (choice1, choice2) {//start function

         var winner; //you don't need to specify 'undefined'

         if (choice1 === choice2) {
             winner = "Tie game!";


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

confirm("You chose: " + choice1+ "\nSystem: " + choice2+ "\nOutcome: " + winner);

    }//end function

Keep in mind keeping your variables locals to a scope better than globals

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.