Always a TIE. Can't understand why choice2 value doesn't passed


#1

Here my code. Help please:)

var userChoice = prompt("Do you choose rock, paper or scissors?");
var choice1=userChoice;
console.log("User:"+" "+choice1);

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 choice2= 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();


#2

You don't have to do this:

and this:

Use userChoice and computerChoice like arguments of the function like this:

compare(userChoice, computerChoice);

#3

That's actually the problem choice1 and choice2 are now used inside the function and outside but they have no connection other then their name (they are 2 independent variables.) So when compare() is called choice1 and choice2 inside the function do not get a value assigned and therefore are both undefined (Test it with console.log(choice1);). So as they are both undefined you will always tie.


#4

Thanks to all! Now it's totally clear!