Where it comes from ? (choice1, choice2)


#1

my code goes ok but my question is when we declare a function var compare with parameters (choice1, choice2) where it come from?
earlier we use userChoice and computerChoice and we defined the conditions for that particular function and not for choice1 & choice2, so how the computer will understand the parameter (choice1, choice2) ?

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 wins";
}
}
};

compare (userChoice,computerChoice)


#2

parameters are just local variables for your function so you can use them as you would use any variable that is declared in your function. So not declaring them anywhere is not a problem because you declared them by using them as parameters. The special thing about them is where they get there values from and that is one important thing that makes functions interesting. They get it from the arguments of your function call:

compare (userChoice,computerChoice)

So here is where the connection is made. The first argument (the stuff in () of the function call) userChoice is assigned to the first parameter (choice1) and the second arguement (computerChoice) is assigned to the second parameter (choice2) (aso if there would be more than 2). So probably the reason why we use different names here is to make clear that we're dealing with 2 different sets of variables and that we use the parameters and not the global variables.
For example for testing purpose you could as well run compare("rock","rock") without ever changing the value of userChoice or computerChoice.


I'm stucked