I have promblem


#1

var compare=function(choice1,choice2){

if(choice1===choice2){

 return "The result is a tie!";    

    }
    else if(choice1==="rock"){

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

        }

else{

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

       if(choice2 === "rock"){
          return  "paper wins";

           } 
        else{

            return "scissors wins";
            }
        }
         }

compare(userChoice, computerChoice);


#2

I'm guessing you got your brackets wrong. Heres my piece of code:

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

console.log(compare(userChoice, computerChoice));


#3

but it doesn't work,:sweat:


#4

I left comments in your code above, hope it helps :slight_smile:


#5

thank you that is worked


#6

Hello! I keep receiving this error:
rock
You threw: paper
The computer threw: undefined
You won

The computer choise is always 'undefined'... I don't know what the problem is, because I compare my code with the codes of others and can't find any difference.

My code:
function getUserChoice() {
var userInput = prompt("Rock, Paper or Scissors?");
userInput = userInput.toLowerCase();
if (userInput === "rock" || userInput === "paper" || userInput === "scissors") {
return userInput;
} else {
console.log("Error!");
}
}

function getComputerChoice() {
var randomNumber = Math.floor(Math.random()*3);
switch (randomNumber) {
case 0: console.log("rock");
break;
case 1: console.log("paper");
break;
case 2: console.log("scissors");
break;
}
}

function determineWinner(userChoice, computerChoice) {
if (userChoice === computerChoice) {
return "The game is tie";
}
if (userChoice === "rock") {
if (computerChoice === "paper") {
return "Computer won!";
} else {
return "You won!";
}
}
if (userChoice === "paper") {
if (computerChoice === "scissors") {
return "Computer won!";
} else {
return "You won!";
}
}
if (userChoice === "scissors") {
if (computerChoice === "rock") {
return "Computer won!";
} else {
return "You won!";
}
}
}

function playGame() {
var userChoice = getUserChoice();
var computerChoice = getComputerChoice();
console.log('You threw: ' + userChoice);
console.log('The computer threw: ' + computerChoice);
console.log(determineWinner(userChoice, computerChoice));
}

playGame();