Can any one tell whats wrong with this code it is showing error choice1 not defined


#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 "scissors win";
}
}
};
userChoice(choice1);
computerChoice(choice2);


#2

Hi this part

userChoice(choice1);
computerChoice(choice2);

you should call the function compare() with userChoice and computerChoice


#3

Can you please present how to call the function? You mean like this
var compare = function (userChoice, computerChoice);


#4

@alfanjes
It's not necessary. When you create a function, the pairs of brackets are used for putting optional parameters that will be input to represent its value (That's why functions exist! Depending on which parameter you input, a different output will come!). In this case your code:

It just adds parameters that will be different when this function is going to be called. The parameters inside of it are just used like variable you input inside the parentheses. So putting choice1 and choice2 in the parentheses doesn't change as long as when your function is called, the parameters are userChoice and computerChoice.


#5

In this case

var compare = function (userChoice, computerChoice);

You create a function but if you want to call the function just add at the end of your code

compare(userChoice, computerChoice)

#6

Thank you for explanation.