What if choice1 is scissors?


#1

Here is my code:

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

choice1 = userChoice;
choice2 = computerChoice;

function compare(choice1, choice2){
if(choice1===choice2){
return "The result is a tie!";
}
if(choice1==="rock"){
if(choice2==="scissors"){
return ("rock wins!");
}else{
return("paper wins!");
}
}
if(choice1==="paper"){
if(choice2==="rock"){
return ("paper wins!");
}else{
return("scissors wins!");
}
}
if(choice1==="scissors"){
if(choice2==="rock"){
return ("rock wins!");
}else{
return("scissors wins!");
}
}
}

console.log(compare(choice1,choice2));

The error is:
SyntaxError: Unexpected token }


#2

Hmm Something wrong here...(Should delete one of the brackets)


#3

I am very new to programming, and can't find the problem :confused:


#4

He told you the problem...?


#5

It still does not work with his fix


#6

What is the new error?


#7

Same error
SyntaxError: Unexpected token }


#8

Post the updated code and please format it this time.


#11

Don't know why it's not letting me post it correct
´´´
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);
}

choice1 = userChoice;
choice2 = computerChoice;

function compare(choice1, choice2){
if(choice1===choice2){
return "The result is a tie!";
}
if(choice1==="rock"){
if(choice2==="scissors"){
return ("rock wins!");
}else{
return("paper wins!");
}
}
if(choice1==="paper"){
if(choice2==="rock"){
return ("paper wins!");
}else{
return("scissors wins!");
}
}
if(choice1==="scissors"){
if(choice2==="rock"){
return ("rock wins!");
}else{
return("scissors wins!");
}
}
}

console.log(compare(choice1,choice2));
}
´´´


#12



#13

Hi there

for me it worked when I didn't use the choice1 and choice2 rather userChoice where ever there is choice1 and cumputerChoice for choice2. See below my code:

var userChoice = prompt("Do you choose rock, paper or scissors?");

var computerChoice = Math.random();
console.log (computerChoice);

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 (userChoice,computerChoice) {
if (userChoice === computerChoice) {
return ("The result is a tie!");
}
else if (userChoice === "rock"){
if (computerChoice === "scissors"){
return ("rock wins");
}
else {
return ("paper wins");
}
}

else if (userChoice === "paper"){
    if (computerChoice === "rock") {
        return ("paper wins");
    }
    else {
        return ("scissors wins");
    }

}
else if (userChoice === "scissors"){
    if (computerChoice === "rock") {
        return ("rock wins");
    }
    else {
        return ("scissors wins");
    }

}

}
compare (userChoice , computerChoice);