Rock, Paper, Scissors


#1

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(userChoice === "scissors")
{if(computerChoice === "rock")
    {return "rock wins";}
    else {return "scissors wins";};
    };

};

userChoice(rock);
computerChoice();


#2

I am wondering why this doesn't work?


#3

I have also already changed the last userchoice and computerchoice to choice1 and choice2 to match the rest. It still gives me an error of syntax.


#4

Your last couple lines are actually the problem:

userChoice(rock);
computerChoice();

What you should put is this:

compare(userChoice, computerChoice);

#5

So I changed it to compare(userChoice, computerChoice);

but it returns this: "SyntaxError: Unexpected keyword 'else'"


#6

Could you post your updated code for me so we can take a look at it?


#7

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";}
    };

};

compare(userChoice, computerChoice);


#8

why such an error takes off?

Oops, try again. Make sure to call your compare function with userChoice and computerChoice as the arguments!

the code works. and function call:
compare (computerChoice, userChoice);

 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);
 console.log("user: " + userChoice);

 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 === "paper")
         {
          return "scissors wins" ;
         }  
         else 
         {
             return "rock wins";
         }
    }
 }

 compare (computerChoice, userChoice);