How do userChoice and computerChoice "connect"/"relate" to choice1 and choice2?


#1

Okay, so the only thing I can think of is that because the parameters of userChoice and computerChoice are defined before the compare function, when said function kicks in, it sees that there is already a variable with a value of "rock" or "paper" or "scissors" so it automagically assigns it to the parameters...or not, I don't know. It seems sketchy at best.

Please enlighten me. Thank you.


#2

You don't need a function if it is a 1 round game. You should try sth like this if you want it to be a function.

var game = function {
var player = prompt("option");
var computer = Math.random();
// then write an if else if  else to change computer to a rock paper scissors.
//lastly write an if else if else to compare.
}
game();

You could also compare inside a function and change it to parameters, but it is of no use I believe.

var game = function () {
    var player = prompt("option");
    var computer = Math.random();
    // then write an if else if  else to change computer to a rock paper scissors.
    var comparison = function (x, y){
// write the comparison using x and y parameters instead of player and computer.
}
comparison(player, computer);
    }
    game();

#3

Wait... I think I understood it.... compare(computerChoice, userChoice) means that for choice1 and choice2 the values of the two variables are used-like, "replace choice1 with whatever computerChoice is"? That makes sense..

Why did you define the variables inside the function?


#4

Jep that is how parameters work:

var name = fucntion(para1,para2,para3,...){
   ...
}
name(argument1,argument2, argument3,...);

does actually the same as

    var name = fucntion(){
       var para1 = argument1; 
       var para2 = argument2;
       var para3 = argument3;
       ...
    }
    name();

but the first version is actually better as the second one hard codes a value, meaning that it is a fix value in every function call whereas in the first version you could change the arguments with every function call which makes the function a lot more useful.


#5

I think I'm beginning to understand, although I'll obviously have to observe this in practice. Thank you!