Bacon
Bacon

Reputation: 39

Changing Javascript variable with function?

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>

Upvotes: 0

Views: 49

Answers (3)

Bassam Rubaye
Bassam Rubaye

Reputation: 302

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

Upvotes: 0

Simon Sch&#252;pbach
Simon Sch&#252;pbach

Reputation: 2683

You should change the order of the function calls.

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

Upvotes: 0

Mike Cluck
Mike Cluck

Reputation: 32511

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);

Upvotes: 2

Related Questions