Inappropriate Choice


#1

Hello!

So I finished the "Build Rock, Paper, Scissors" lecture and in the end, there were some extra challenges, among them:
"What if a user makes an inappropriate choice like 'dog'? How can we extend the function to handle that?"
I wanted solve this, and thought that is could be done by defining the all the outcomes of when the user chooses rock, paper or scissors, and then having an else statement accounting for all other things the user could write. (choice2=userchoice in this case)

This is what I wrote:

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 if(choice2 === "paper"){
        return "paper wins";
    }
    else{
        return "Inappropriate choice!";
    }
}
else if (choice1 === "paper"){
    if(choice2 === "rock"){
        return "paper wins";
        }
    else if(chioce2==="scissors"){
        return "scissors wins";
    }
    else{
        return "Inappropriate choice!";
    }
}
else if(choice1 = "scissors"){
    if(choice2 = paper){
        return "scissors wins";
    }
    else if(choice2="rock"){
        return "rock wins"
    }
    else{
        return "Inappropriate choice!";
    }
}        
}

compare(computerChoice, userchoice)

And it is not working. Could somebody point out what's wrong in my thinking and code?
Thank you :slightly_smiling:


#2

It would probably be easier to leave choice1 to be userChoice and just add one else to the chain of else ifs :slightly_smiling:
And the problem in the code is that you're using = (assignment operators) here:

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

instead of comparison operators (== or ===)


#3

As I so often suggest, set this aside until after you complete the entire JavaScript track. If we try to help you now we will be introducing concepts that you have not yet learned being as this challenge appears so early in the track. When you return you will be properly outfitted with the tools and understanding you need to continue on with the project.


#4

Thank you for your help! :slightly_smiling: