Plz help with code! What is choice1 is 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 (choice1 === "scissors") {
if (choice2 === "rock") {
return("rock wins");
} 
else {
 return("paper wins");
}
    }
else
if(choice1 === "scissors") {
    if(choice2 === "rock") {
        console.log("rock wins");
    }
if(choice2 === "paper") {
    console.log("scissors wins");
}
}

};

#2

First thing first. You have 2 sections that state:

if (choice1 === "scissors") {

so you need to delete the last section for this reason and also because it should be an else if rather than an else.

Secondly, in your first:

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

you declared in your else statement that paper beats scissors by saying "paper wins" instead of "scissors wins"

Make these adjustments and count your { }'s to make sure everything opens and closes properly and you should be fine. Good luck


#3

could you elaborate a little please?


#4

OK lets try this (make sure to scroll through the copied 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);

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") { ///choice1 is equal to scissors
if (choice2 === "rock") { ///choice2 is equal to rock
return("rock wins"); ///rock wins
} 
else { ///choice2 would be equal to paper
 return("paper wins"); /// scissors should win against paper
}
    }
else ///this whole next section should be deleted since you covered it all in the last section of code.
if(choice1 === "scissors") {
    if(choice2 === "rock") {
        console.log("rock wins");
    }
if(choice2 === "paper") {
    console.log("scissors wins");
}
}

};

so your updated code with those correction would look like the following and I will add notations for counting your { }'s :

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){ ///opens the function
if(choice1 === choice2){ ///opens the if
    return("The result is a tie!");
} ///closes the if

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

}; ///closes the function

Hope this helps you understand a bit better


#5

oh ok thx a lot!:grinning:


#6

As well...."compare (userChoice, computerChoice);" should be added?