Code works, but I have a question about the compare function


So my code works, and I passed the exercise, but I'm confused about what happens when I call the compare function at the end. It says at the end, "call the compare function and pass it userChoice and computerChoice". Yet, inside the compare function, neither userChoice or computerChoice is ever mentioned. In other ways, JS is so literal. You have to pass it the exact var names for it to work. How does it know that choice1 and choice2 are actually the same as userChoice and computerChoice? I'm having a hard time articulating this question, but any help at an answer would be appreciated.

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"){
        return "paper wins";
            return "scissors wins";


compare(userChoice, computerChoice);


Hey, I think i know what you mean with this question (sorry if i don't answer with what you're after, I'm pretty new to this myself).

Ok I'm having a hard time articulating this answer as well but I'll do my best lol.

So right at the top of the code you made the 2 variables, userChoice and computerChoice. (As far as I am aware) Variables are just a way to sort information, like a number or a string for example, so that you can access it quickly and change many things by only changing the 1 variable.

Then you made the Function 'compare' with the 2 parameters which you named 'choice1' and 'choice2'. These parameters could really be named anything you wanted to name them. The name for the parameters is only used inside that one variable and nowhere else. If you tried to use them outside of the variable the program would not recognise them. The way i find easiest to think about them is to think for them a 'local variables'.

When you actually come to call your 'function' you need to put in 2 parameters because thats how many you made when you created it. The parameters, in theory, could be anything, but obviously in most cases the function would not work because you have only programmed the function, in this case, to work with 3 parameters, rock, paper and scissors.

The reason the the function works with the 2 variables userChoice and computerChoice is because you programmed those 2 functions have the value rock, paper or scissors.

I hope that this answered your question or at the very least helped in someway. :smile:


Ah! Ok, thank you so much. That makes sense to me! The lightbulb switched on!


Awesome! This is a really good answer.

Just some remarks about terminoligy:
Variable: You can think of variables as spaces in your memory. So by using var you say "Give me some space in this region of the memory". And by using a variable name you have a shortcut to referrence this particular area. After this you can easily store a value in this area by using the = operator or fetch the value stored there by using the variable name as an alias for the value that is stored there.

And the other thing is that the corresponding value in the function call are often called "arguments" so:

    var name = function(parameter1,parameter2,...){...}


so what happens roughly, as you explained correctly, is something like this:

    var name = function(){
        var parameter1 = argument1;
        var parameter2 = argument2


Oh nice. Thanks for the corrections. I had heard the term argument before but I wasn't sure what it was for so that's really helpful.

And thinking about variables like that is a lot easier. I'm going to take a note of this so I can come back to it in the future.

Thanks again